Mybatis环境搭建
- 配置依赖
将我们第一次创建项目时注释的关于Mybatis与JDBC依赖打开。
复制代码 4.0.0 online.suiyu springboot-demo 0.0.1-SNAPSHOT jar springboot-demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java runtime org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test io.springfox springfox-swagger2 2.5.0 io.springfox springfox-swagger-ui 2.5.0 org.springframework.boot spring-boot-maven-plugin
- 修改Controller添加访问路径
package online.suiyu.springbootdemo.controller;@Api("测试类")@RestController@RequestMapping("index")public class IndexController { @Autowired private UserService userService; @ApiOperation(value = "index", notes = "测试方法", tags = { "hello"}) @RequestMapping(value = "/hello", method = RequestMethod.GET) public String index() { return "Hello,World!"; } @ApiOperation(value = "getAllUser", notes = "获取所有的内容", tags = "user") @RequestMapping(value = "/getalluser", method = RequestMethod.GET) public ListgetAllUser() { return userService.getAllUser(); } @ApiOperation(value = "getUserBySex", notes = "通过性别获取user", tags = "user") @RequestMapping(value = "/getuserbysex", method = RequestMethod.GET) public List getUserBySex(@RequestParam(value = "sex", required = true) String sex) { return userService.getUserBySex(sex); }}复制代码
对于上方代码,IDEA针对@Autowired
报了一个警告Spring Team recommends "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies".
Spring推荐使用构造器来注入bean 所以,程序中的bean引入应该写成这样:
private final UserDao userDao;@Autowiredpublic UserServiceImpl(UserDao userDao) { this.userDao = userDao;}复制代码
了解更多,
- 修改service
package online.suiyu.springbootdemo.service;@Servicepublic class UserService { @Autowired private UserMapper userMapper; public ListgetAllUser() { return userMapper.getAllUser(); } public List getUserBySex(String sex) { return userMapper.getUserBySex(sex); }}复制代码
- 创建mybatis的xml配置文件
namespace
名称需要与对应的DAO接口对齐
复制代码
- 创建与xml配置文件对应的DAO接口
getUserBySex()
方法使用了注解形式,简单的增删改查可以使用注解方式。
package online.suiyu.springbootdemo.mapper;@Componentpublic interface UserMapper { ListgetAllUser(); @Select("select * from user where sex = #{sex}") List getUserBySex(String sex);}复制代码
- 在入口类中添加
@MapperScan
@SpringBootApplication@MapperScan("online.suiyu.springbootdemo.mapper")public class SpringbootDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootDemoApplication.class, args); }}复制代码
- 在配置文件
application.yml
中配置Mybatis
mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: online.suiyu.springbootdemo.vo复制代码
可以看出配置的主要是resource中的Mapper.xml文件与javabean文件。
至此,SpringBoot整合Mybatis完成,其项目结构为
如果出现异常,一般都是接口与xml文件对不齐。
添加数据库连接池druid
- 添加druid的maven依赖
复制代码 com.alibaba druid 1.1.10
- 配置数据库连接池的参数
为程序体添加事务管理
- 在主程序中添加注解
@EnableTransactionManagement
开启事务管理 - 在所需的方法体上加上
@Transactional
即可
@Override @Transactional public boolean addUsers(ListuserList) { logger.info("UserServiceImpl.addUsers开始"); int num = userDao.addUsers(userList);// int i = 10 / 0; logger.info("UserServiceImpl.addUsers结束"); return num > 0; }复制代码