springboot+mybatis+thymeleaf架构

Posted 十一路客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot+mybatis+thymeleaf架构相关的知识,希望对你有一定的参考价值。

一.项目结构图

二. 结构剖析

(1)数据库+mybatis配置

https://blog.csdn.net/xyr05288/article/details/84841133

(2)thymeleaf配置

spring.thymeleaf.check-template-location=true
#spring.thymeleaf.prefix=classpath:/static/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
# /html # ;charset=<encoding> is added
#spring.thymeleaf.content-type=text
# set to false for hot refresh
spring.thymeleaf.cache=false

# upload maxfilesize and maxRequestSize
multipart.maxFileSize=50Mb
multipart.maxRequestSize=50Mb

(3)邮件配置

https://blog.csdn.net/xyr05288/article/details/84842475

(4)日志配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <property name="COM_APP_LOG_FILE" value="var/log/myproject/app" />
    <appender name="COM_APP_LOG_FILE_APPENDER"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>%dyyyy-MM-dd HH:mm:ss.SSS %5p $PID:-  [%t] --- %-40.40logger39 [%file:%line] : %msg%n</pattern>
        </encoder>
        <!-- 20151123 配置每天产生一个日志文件 -->
        <Prudent>true</Prudent> 
		<rollingPolicy
		   class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			   <FileNamePattern>
			        $COM_APP_LOG_FILE.%dyyyy-MM-dd.log
			   </FileNamePattern>
			   <!-- keep 60 days worth of history --> 
			   <MaxHistory>60</MaxHistory>
		 </rollingPolicy>
    </appender>
    
    <logger name="com.test.myproject" level="DEBUG">
       <appender-ref ref="COM_APP_LOG_FILE_APPENDER" />
    </logger>

</configuration>

(5)端口配置

#app.runMode=prod
app.runMode=dev
app.workDir=./var

################################################################################
## embedded servlet container
app.server.port=18086
# sessionTimeout in seconds
app.server.sessionTimeout=30000
## reserved
endpoints.shutdown.enabled=true
logging.config=./config/logback.xml
management.port=28086

(6)过滤器和拦截器配置

https://blog.csdn.net/xyr05288/article/details/84842655

(7)AOP打印日志

//20180910 实现web层的日志切面
@Aspect
@Component
public class WebLogAspect 
   private Logger logger = LoggerFactory.getLogger(getClass());
   
   @Pointcut("execution(public * com.myproject.web.security.ToolController.*(..))")
   public void webLog()
   
   @Before("webLog()")
   public void doBefore(JoinPoint joinPoint) throws Throwable
	   //接收到请求 记录请求内容
	   ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
	   HttpServletRequest request = attributes.getRequest();
	   
	   
	   //记录下请求内容
	   logger.info("=====================weblogaspect doBefore==================");
	   logger.info("HTTP_METHOD: " + request.getMethod());
	   logger.info("IP: " + request.getRemoteAddr());
	   logger.info("CLASS: " + joinPoint.getTarget().getClass().getName());
	   logger.info("CLASS_METHOD: " + joinPoint.getSignature().getDeclaringTypeName() + ", " + joinPoint.getSignature().getName());
	   logger.info("ARGS: " + Arrays.toString(joinPoint.getArgs()));
   
   
   @After("webLog()")
   public void doAfter() throws Throwable
	   logger.info("=====================weblogaspect doAfter==================");
	   
   
   
   @AfterReturning(returning="ret", pointcut="webLog()")
   public void doAfterReturning(Object ret) throws Throwable
	   logger.info("=====================weblogaspect doAfterReturning==================");
	   //处理完请求  返回内容
	   logger.info("RESPONSE: " + ret);
	  
   

(8)junit测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes=App.class, webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT)
//@ContextConfiguration(locations = "/spring/applicationContext.xml")
public class SecurityDaoImplTest  
    
	 @Autowired
     private ApiDao apiDao;
	
	@Before(value = "")
	public void before()
		System.out.println("========before test=========");
	
	
	@Test
	public void testSelectPaginationData()
		 HashMap<String, Object> param = new HashMap<String, Object>();
			String page_size = "10";
			String page_number = "1";
			String searchKeywords = "";
				
			System.out.println("page_size: " + page_size + "; page_number: " + page_number
					   + "; searchKeywords: " + searchKeywords);
			
			param.put("page_size", page_size);
			param.put("page_number", page_number);			
			param.put("searchKeyWords", searchKeywords);		
			HashMap<String, Object> map = new HashMap<String,Object>();		
		    map = this.apiDao.selectPaginationProjectsByKeywords(param);
		    
		    System.out.println("result: " + JSON.toJSONString(map));
	

(9)pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.myproject</groupId>
  <artifactId>myproject</artifactId>
  <version>1.0</version>
  <properties>
		<java.version>1.7</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<start-class>com.myproject.App</start-class>
	</properties>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.3.RELEASE</version>
	</parent>
	<dependencies>
		<!-- spring boot libraries -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>		
		<!-- 20180910 测试aop -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-aop</artifactId>
		</dependency>		
		<!-- 20181023 测试redis -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>	
		<!-- vendor libraries -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.1.41</version>
		</dependency>
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>18.0</version>
		</dependency>
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc14</artifactId>
			<version>10.2.0.1.0</version>
		</dependency>
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
		</dependency>
		<dependency>
			<groupId>org.nutz</groupId>
			<artifactId>ssdb4j</artifactId>
			<version>9.2</version>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.2.2</version>
		</dependency>	
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
		</dependency>
		<!-- 20151012 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
	    </dependency>		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>		
		<dependency>
			<groupId>net.sourceforge.jexcelapi</groupId>
			<artifactId>jxl</artifactId>
			<version>2.6.12</version>
		</dependency> 
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>	
		<dependency>
			<groupId>org.apache.ant</groupId>
			<artifactId>ant</artifactId>
			<version>1.8.2</version>
		</dependency>		
		<!-- 20170504 add ehcache 缓存测试 缓存依赖spring-context-support;slf4j-->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-context-support</artifactId>
		    </dependency>	
		<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache 用2.10.3会出错
		     随后换成了2.10.1-->
		<dependency>
		    <groupId>net.sf.ehcache</groupId>
		    <artifactId>ehcache</artifactId>
		    <version>2.10.3</version> 
		 </dependency>	
		<dependency>
			<groupId>com.sun.mail</groupId>
			<artifactId>javax.mail</artifactId>
			</dependency>
	</dependencies>	  
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

 

以上是关于springboot+mybatis+thymeleaf架构的主要内容,如果未能解决你的问题,请参考以下文章

基于 SpringBoot 的仿豆瓣平台源码分享

基于 SpringBoot 的仿豆瓣平台源码分享

3.springboot+Thymeleaf

springBoot引入thymyleaf

Spring Boot 入门之 Web 篇

springboot中模板 freemark,thymeleaf,jsp