spring boot + mybatis + jsp整合

spring boot + mybatis + jsp整合
-----------------------------


1.pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.imitate</groupId>
<artifactId>imitate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>


<properties>
<!-- 主要依赖库的版本定义 -->
<slf4j.version>1.7.22</slf4j.version>
<commons-lang3.version>3.2.1</commons-lang3.version>
<poi.version>3.7</poi.version>
<mysql-connector.version>5.1.28</mysql-connector.version>

<!-- Plugin的属性定义 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>

</properties>


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>


<!-- 依赖项定义 -->
<dependencies>
  <!-- spring boot  -->
<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</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-configuration-processor</artifactId> 
   <optional> true </optional> 
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>

<!-- GENERAL UTILS begin -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
 <groupId>commons-io</groupId>
 <artifactId>commons-io</artifactId>
 <version>1.3.2</version>
</dependency>
<dependency>
 <groupId>commons-fileupload</groupId>
 <artifactId>commons-fileupload</artifactId>
 <version>1.2.1</version>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.1.40</version>
  </dependency>
<!-- GENERAL UTILS end -->

<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- servlet -->

<!-- 嵌入式tomcat,少了这个,不能用JSP -->
<dependency>  
   <groupId>org.springframework.boot</groupId>  
   <artifactId>spring-boot-starter-tomcat</artifactId>  
</dependency>  
<dependency>  
   <groupId>org.apache.tomcat.embed</groupId>  
   <artifactId>tomcat-embed-core</artifactId>  
</dependency>  
<dependency>  
   <groupId>org.apache.tomcat.embed</groupId>  
   <artifactId>tomcat-embed-el</artifactId>  
</dependency>
<dependency>  
   <groupId>org.apache.tomcat.embed</groupId>  
   <artifactId>tomcat-embed-jasper</artifactId> 
   <scope>provided</scope>
</dependency>

<!-- slf4j -->
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
</dependency>

<!-- mysql connector-->
   <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
  </dependency>
       <dependency>
            <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-jdbc</artifactId>
       </dependency>
  <!-- mybatis-springboot -->
<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.0</version>
</dependency>
</dependencies>
</project>


2.目录结构:



3.配置文件:
application.properties:通用配置
spring.profiles.active=dev
spring.mvc.view.prefix=WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
application-dev.properties:开发环境配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://***
spring.datasource.username=***
spring.datasource.password=***
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
#####配置上传下载#########
multipart.maxFileSize=50Mb
multipart.maxRequestSize=50Mb
##扫描mapper.xml
mybatis.mapper-locations=classpath:mapper/*.xml
server.port=8080
application-prod.properties:生产环境配置
默认通过application-xxx.properties识别;
springmvc中大部分配置在spring boot中都是在.properties和注解中完成
4.启动类:ImitateApplication.class
package com.imitate;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.test.context.ContextConfiguration;


@SpringBootApplication
@ContextConfiguration
@EnableAutoConfiguration
@EnableConfigurationProperties(ImitateProperties.class)
@MapperScan("com.imitate.dao")
public class ImitateApplication {


public static void main(String[] args) {
SpringApplication.run(ImitateApplication.class, args);
}


}


5.mapper.xml文件还是放在resources下的mapper文件夹下
6.dao:
package com.imitate.dao;


import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;


//@Mapper
public interface DealOrderDao{

// @Select("select count(*) from deal_order a where a.`status`='0' ")
int queryDealCount();


}
如果不用mybatis配置文件的话,可以直接使用@Mapper注解完成sql


7.service跟springmvc一样
8.controller:
@RequestMapping(value="index")
@Controller
public class ImitateController {

@Autowired
DealOrderServiceImpl service;

@RequestMapping(method=RequestMethod.GET)
public ModelAndView hello(){
ModelAndView mv=new ModelAndView("index");
int count = service.queryCount();
mv.addObject("count", count);
return mv;
}

}

想要跳转jsp的话就得用servlet的ModelAndView,或者直接弃用jsp选择其他视图层模版
9.jsp

放的位置根据spring.mvc.view.prefix,webapp/WEB-INF/JSP/index.jsp


10.测试类:默认与boot同包

package com.imitate.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import com.imitate.dao.DealOrderDao;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class AppTest {

@Autowired
DealOrderDao dealOrderDao;

@Test
public void testDealOrder(){
int count = dealOrderDao.queryDealCount();
System.out.println(count);
}




}


11.读取.properties配置参数
以前用@Configuration+@value("${}");
现在推荐@ConfigurationProperties(prefix="com.puruang.imitate"):
package com.imitate;

import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="com.puruang.imitate")
@Data
public class ImitateProperties {

private String name;
}

12.附:spring boot 多配置优先级(由高到低):
1 命令行参数
2 来自java:comp/env的JNDI属性
3 Java系统属性(System.getProperties())
4 操作系统环境变量
5 RandomValuePropertySource配置的random.*属性值
6 jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
7 jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
8 jar包外部的application.properties或application.yml(不带spring.profile)配置文件
9 jar包内部的application.properties或application.yml(不带spring.profile)配置文件
10 @Configuration注解类上的@PropertySource
11 通过SpringApplication.setDefaultProperties指定的默认属性


猜你喜欢

转载自blog.csdn.net/hansplay/article/details/78672216