说明:该案例的开发环境是idea2017+maven3+SpringBoot1.5.9+Mybatis3+MySQL5.7
最终的案例目录结构如下所示:
一、添加依赖
在pom.xml文件中,加入springboot依赖、日志文件依赖、tomcat依赖、servlet依赖、jstl依赖、mybatis-spring-boot依赖、jdbc依赖
org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE 4.0.0 com.yunlian.springboot boot-mybatis war 1.0-SNAPSHOT boot-mybatis http://maven.apache.org UTF-8 junit junit 4.12 test org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-logging org.springframework.boot spring-boot-starter-log4j2 org.springframework.boot spring-boot-starter-tomcat org.apache.tomcat.embed tomcat-embed-jasper javax.servlet javax.servlet-api javax.servlet jstl org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 org.mybatis mybatis 3.4.3 mysql mysql-connector-java 5.1.43 boot-mybatis org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 1.8 UTF-8
二、mybatis、db配置
在application.properties文件中,配置如下信息:
# 配置jsp文件的位置,默认位置为:src/main/webapp spring.mvc.view.prefix=/ # 配置jsp文件的后缀 spring.mvc.view.suffix=.jsp #Spring Boot中的乱码和编码问题 spring.http.encoding.force=true #除了常见的 http encoding,Spring Boot中还可以控制这些编码 #banner.charset #spring.freemarker.charset #server.tomcat.uri-encoding #spring.mail.default-encoding #spring.messages.encoding #spring.thymeleaf.encoding ######################################################## ###datasource ######################################################## spring.datasource.url = jdbc:mysql://localhost:3306/spring-boot spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver #spring.datasource.max-active=20 #spring.datasource.max-idle=8 #spring.datasource.min-idle=8 #spring.datasource.initial-size=10 ######################################################## ### Mybatis 配置 ######################################################## mybatis.typeAliasesPackage=com.yunlian.entity mybatis.mapperLocations=classpath:com/yunlian/dao/*.xml
三、创建UserDao接口
在dao层创建UserDao接口,并在接口上加上注解Mapper,有了Mapper注解,可以将mybatis完美的融合进springboot中:
package com.yunlian.dao; import com.yunlian.entity.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserDao { User findOne(String id); User save(User user); }
四、创建映射文件
在resources目录下面,创建与UserDao.java相同的包目录,在这目录下面创建名为UserDao.xml的映射文件,映射文件的内容如下:
五、创建业务类
在service包下面创建一个UserService接口,并在service.impl包下面创建UserService接口的实现类UserServiceImpl,并在实现类中注入UserDao的bean,如下所示:
package com.yunlian.service; import com.yunlian.entity.User; public interface UserService { User getUser(); void save(User user); }
package com.yunlian.service.impl; import com.yunlian.dao.UserDao; import com.yunlian.entity.User; import com.yunlian.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; @Override public User getUser() { User user = userDao.findOne("09e7b25c-12f7-43ea-a553-fb34189fce17"); return user; } @Override public void save(User user) { User u = userDao.save(user); return; } }
六、创建控制层
在controller包下面创建一个UserController类,并在这个类中注入业务类的bean,如下所示:
package com.yunlian.controller; import com.yunlian.entity.User; import com.yunlian.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/get") @ResponseBody public User getUser() { User user = userService.getUser(); return user; } }
七、配置程序入口类
在入口程序类上加配置注解信息,如下所示:
package com.yunlian.controller; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan("com.yunlian")//包名:扫描这个包下面的加了@Controller注解的类及其子包的加了@Controller注解的类,或者加了Service注解等其他组件注解的类 @MapperScan("com.yunlian.dao")// mapper 接口类扫描包配置 public class SpringBootController { public static void main(String[] args) throws Exception { //程序启动入口,一般该入口文件不写成控制层 SpringApplication.run(SpringBootController.class, args); } }
八、日志文件
在resources目录下面创建日志文件log4j2-spring.xml,其内容如下:
%d [%t] %-5p [%c] - %m%n %d [%t] %-5p [%c] - %m%n
九、运行访问
运行程序入口类的main,运行好后,即可访问 http://localhost:8080/user/get
可以在页面上看到返回的 json 数据