SpringBoot 集成Mybatis
1,必要的依赖:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 --> <java.version>1.8</java.version> </properties> <!-- spring-boot-starter-web: MVC,AOP的依赖包.... --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- mysql 依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
</dependencies>
2, mysql 数据库 testdb 下有张user 表:
3,表对应的实体类:
import lombok.Data;
@Data //相当于@Setter @Getter public class User { private int id; private String username; private String sex; private Date birthday; private String address; }
4,mapper 接口(dao)
package com.baiyue.mappers; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import com.baiyue.entity.User; public interface UserMapper { void insertUser(User user); User selectUserById(int id); List<User> selectAllUser(); // 模糊查询的 List<User> selectUserNameLike(String username); List<User> selectByName(String username); void deleteUserById(int id); // 多参数 传递方式1 // public void updateUserById(@Param("id")int id,@Param("username")String // username); // 多参数 传递方式2 void updateUserById(Map<String, Object> map); // 使用注解 字符串替换 不用写根据id查 根据username查 根据address 查 @Select("select * from user where ${column} = #{value}") List<User> findByColumn(@Param("column") String column, @Param("value") String value); // resultMap 结果映射 只想输出部分的属性 User selectUserByIdMap(int id); // 动态sql SELECT * FROM testdb.user where username='Linda' and sex='male'; List<User> selectUsers(Map<String, String> map); void update(String username, String sex, int age); }
5,mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.baiyue.mappers.UserMapper"> <!-- 根据 id 查询 user 表中的数据 id:唯一标识符,此文件中的id值不能重复 resultType:返回值类型,一条数据库记录也就对应实体类的一个对象 parameterType:参数类型,也就是查询条件的类型 --> <select id="selectUserById" resultType="User" parameterType="int"> <!-- 这里和普通的sql 查询语句差不多,对于只有一个参数,后面的 #{id}表示占位符,里面不一定要写id,写啥都可以,但是不要空着,如果有多个参数则必须写pojo类里面的属性 --> select * from user where id = #{id} </select> <insert id="insertUser" parameterType="com.baiyue.entity.User"> insert into user(id,username,sex,birthday,address) value(#{id},#{username},#{sex},#{birthday},#{address}) </insert> <select id="selectAllUser" resultType="com.baiyue.entity.User"> select * from user </select> <select id="selectUserNameLike" parameterType="java.lang.String" resultType="com.baiyue.entity.User"> select * from user where username like '%${value}%' </select> <select id="selectByName" parameterType="java.lang.String" resultType="com.baiyue.entity.User"> select * from user where username = #{username} </select> <delete id="deleteUserById" parameterType="int"> delete from user where id=#{id} </delete> <update id="updateUserById" parameterType="java.util.Map"> update user set username=#{username} where id=#{id} </update> <!-- resultMap --> <resultMap id="userResultMap" type="com.baiyue.entity.User"> <id property="id" column="id" /> <result property="username" column="username" /> <result property="address" column="address" /> </resultMap> <select id="selectUserByIdMap" resultMap="userResultMap" parameterType="int"> <!-- 数据库address 对应address_city --> select id,username,address from user where id =#{id} </select> <select id="selectUsers" resultType="com.baiyue.entity.User" parameterType="java.util.Map" > select * from user where username =#{username} <if test="sex != female"> AND sex =#{sex} </if> </select> </mapper>
6,配置文件链接mapper 和 mapper.xml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/testdb
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
# mybatis
mybatis:
mapper-locations: classpath:com/baiyue/mapping/*Mapper.xml
type-aliases-package: com.baiyue.entity
7, service 层
package com.baiyue.service; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.baiyue.entity.User; import com.baiyue.mappers.UserMapper; import com.github.pagehelper.PageInfo; @Service public class UserService { @Autowired private UserMapper userMapper; public void insertUser(User user) { userMapper.insertUser(user); }; public User selectUserById(int id) { User user = userMapper.selectUserById(id); return user; }; public List<User> selectAllUser() { List<User> list = userMapper.selectAllUser(); return list; }; // 模糊查询的 public List<User> selectUserNameLike(String username) { List<User> list = userMapper.selectUserNameLike(username); return list; }; public List<User> selectByName(String username) { List<User> list = userMapper.selectByName(username); return list; }; public void deleteUserById(int id) { userMapper.deleteUserById(id); }; // 多参数 传递方式1 // public void updateUserById(@Param("id")int id,@Param("username")String // username); // 多参数 传递方式2 public void updateUserById(Map<String, Object> map) { userMapper.updateUserById(map); }; // 使用注解 字符串替换 不用写根据id查 根据username查 根据address 查 List<User> findByColumn(String column, String value) { List<User> list = userMapper.findByColumn(column, value); return list; }; // resultMap 结果映射 只想输出部分的属性 public User selectUserByIdMap(int id) { User user = userMapper.selectUserByIdMap(id); return user; }; // 动态sql SELECT * FROM testdb.user where username='Linda' and sex='male'; public List<User> selectUsers(Map<String, String> map) { List<User> list = userMapper.selectUsers(map); return list; }; public void update(String username, String sex, int age) { userMapper.update(username, sex, age); }; }
8,controller 层:
@RestController public class Mybatis { @Autowired private UserService userService; @RequestMapping("/insertUser") public String insertUser(){ User user = new User(); user.setUsername("chris"); user.setSex("male"); userService.insertUser(user); return "success"; } }
7,启动类,要加上mapper 的扫包:
@SpringBootApplication
@ComponentScan("com.baiyue.*") //因为有一些bean 不在当前包或者子包下面,所以@SpringBootApplication 会有些bean 扫不到 @MapperScan("com.baiyue.mappers") public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
在集成mybatis的基础上,使用分页插件:
<!-- springboot 整合 pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency>
service 层添加方法:
//使用pageHelper 分页插件 page是页码 size 是个数 public PageInfo<User> selectUserByPagehelper(int page,int size){ PageHelper.startPage(page, size); //底层会将sql 分装,直接搜索条件中的页数,以及数量 List<User> list = userMapper.selectAllUser(); //前后都是一样的代码可以用AOP进行封装 PageInfo<User> pageInfo = new PageInfo<>(list); return pageInfo; }
controller 层:
@RequestMapping("/selectUserByPagehelper") public PageInfo<User> selectUserByPagehelper(int page,int size){ User user = new User(); PageInfo<User> pageInfo = userService.selectUserByPagehelper(page, size); return pageInfo; }
Aimer,c'est partager