本文共 6018 字,大约阅读时间需要 20 分钟。
用完spring-data-jpa之后并不是很想用mybatis,但是没办法呀,大环境还是mybatis,而且现在mybatis也出了不少插件,我们还是一起看看如何整合mybatis吧
关于整合mybatis有两种方式,一种是注解方式,另一种是传统的xml方式先看看引入的依赖
org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test
sql文件
CREATE TABLE `test`.`Untitled` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `passwd` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql:///test?useSSL=truespring.datasource.username=rootspring.datasource.password=root#打印sql语句到控制台logging.level.com.priv.gabriel.demoformybatis.mapper=debug
User.java
package com.priv.gabriel.demoformybatis.entity;/** * Created with Intellij IDEA. * * @Author: Gabriel * @Date: 2018-10-14 * @Description: */public class User { private long id; private String username; private String passwd; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; }}
UserMapper.java
package com.priv.gabriel.demoformybatis.mapper;import com.priv.gabriel.demoformybatis.entity.User;import org.apache.ibatis.annotations.*;/** * Created with Intellij IDEA. * * @Author: Gabriel * @Date: 2018-10-14 * @Description: */@Mapperpublic interface UserMapper { /*注解方式的话直接在方法上写上对应的sql语句就可以了*/ @Select("select * from user where id = #{id}") /*如果需要重复使用该Result,给该Results加一个 id 属性,下面即可使用@ResultMap(id)重复调用*/ @Results(id = "user",value = { @Result(property = "username",column = "name") }) User findById(long id); /*获取回传自增id*/ /*id会自动存入user中*/ @Insert("insert into user(name,passwd) values (#{username},#{passwd})") @Options(useGeneratedKeys = true,keyProperty = "id") int inertUser(User user);}
UserController.java
package com.priv.gabriel.demoformybatis.controller;import com.priv.gabriel.demoformybatis.entity.User;import com.priv.gabriel.demoformybatis.mapper.UserMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import java.util.List;/** * Created with Intellij IDEA. * * @Author: Gabriel * @Date: 2018-10-14 * @Description: */@RestController@RequestMapping("/users")public class UserController { @Autowired private UserMapper userMapper; @RequestMapping(value = "/{id}",method = RequestMethod.GET) public User selectById(@PathVariable long id){ return userMapper.findById(id); } @RequestMapping(value = {""},method = RequestMethod.POST) public String addUser(User user){ userMapper.inertUser(user); return "现在自增id为"+user.getId(); }}
在SpringBoot1.x中还需要在启动类中加入@MapperScan("mapper所在目录")
,而在2.x版本中不需要加入就可以自动扫描到mapper了
需要引入pageHelper依赖
com.github.pagehelper pagehelper-spring-boot-starter 1.2.5
在UserMapper中新增一个查询所有信息的方法
@Select("select * from user") /*调用之前的Results*/ @ResultMap("user") ListlistUser();
在Controller中调用
/*获取分页数据 包含分页详细信息*/ @RequestMapping(value = "/",method = RequestMethod.GET) public PageInfo getAll(@RequestParam Integer page, @RequestParam Integer size){ PageHelper.startPage(page,size); Listusers = userMapper.listUser(); PageInfo pageInfo = new PageInfo(users); return pageInfo; } /*仅获取分页数据 @RequestMapping(value = "/",method = RequestMethod.GET) public List getAll(@RequestParam Integer page, @RequestParam Integer size){ PageHelper.startPage(page,size); List users = userMapper.listUser(); return users; }*/
xml方式的话和以前Spring整合mybatis没有太大的区别,在这里可以使用Mybatis-Generator自动生成代码少些一点代码,其余的话就不多介绍了
不管是使用注解方式还是XML方式都还是会存在一个什么都得自己动手的情况,接下来的话,我们整合一个通用的Mapper,在mybatis中体验jpa的感觉
引用依赖tk.mybatis mapper-spring-boot-starter 2.0.4
继承BaseMapper
package com.priv.gabriel.demoformybatis.mapper;import com.priv.gabriel.demoformybatis.entity.User;import org.apache.ibatis.annotations.Mapper;import tk.mybatis.mapper.common.BaseMapper;/** * Created with Intellij IDEA. * * @Author: Gabriel * @Date: 2018-10-14 * @Description: */@Mapperpublic interface UserCommonMapper extends BaseMapper{}
接着在Controller中调用
@Autowired private UserCommonMapper mapper; @RequestMapping("/testCommonMapperForSelectAll") public ListtestCommonMapper(){ return mapper.selectAll(); }
关于mybatis的扩展很多,还有像mybatis-plus,关于这个话有时间再说吧,ε=ε=ε=┏(゜ロ゜;)┛
转载地址:http://urjbx.baihongyu.com/