一、引入mybatis相关jar包
pom.xml
<!-- 集成mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- 引入mysql连接jar包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
二、创建相关包
结构如下:
四、完善相关代码
mapper文件: UserDao.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.example.springboot.dao.UserDao"> <resultMap id="userResultMap" type="User"> <result property="id" column="id"/> <result property="username" column="username"/> <result property="password" column="password"/> </resultMap> <sql id="userColumns"> id , username , password </sql> <select id="get" parameterType="long" resultMap="userResultMap"> select <include refid="userColumns"/> from user where id = #{id} </select> <select id="find" parameterType="java.util.Map" resultMap="userResultMap"> select <include refid="userColumns"/> from user <where> <if test="id != null"> id = #{id} </if> <if test="username != null"> username = #{username} </if> <if test="password != null"> password = #{password} </if> </where> </select> <select id="page" parameterType="java.util.Map" resultMap="userResultMap"> select <include refid="userColumns"/> from user <where> <if test="id != null"> id = #{id} </if> <if test="username != null"> username = #{username} </if> <if test="password != null"> password = #{password} </if> </where> LIMIT #{_start},#{_pageSize} </select> <select id="total" parameterType="java.util.Map" resultType="int"> select count(1) from user <where> <if test="id != null"> id = #{id} </if> <if test="username != null"> username = #{username} </if> <if test="password != null"> password = #{password} </if> </where> </select> <insert id="save" parameterType="User"> insert into user( #{id}, #{username}, #{password} ) </insert> <update id="update" parameterType="User"> update user <set> <if test="username != null"> username = #{username} </if> <if test="password != null"> password = #{password} </if> </set> where id = #{id} </update> <delete id="delete" parameterType="long"> delete from user where id=#{id} </delete> </mapper>
dao 文件: UserDao.java
package com.example.springboot.dao; import com.example.springboot.entity.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; import java.util.Map; @Mapper public interface UserDao { User get(Long id); List<User> find(Map map); int total(Map map); boolean save(User user); boolean update(User user); int delete(Long id); }
entity文件:User.java
package com.example.springboot.entity; public class User { private Long id; private String username; private String password; 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 getPassword(){ return password; } public void setPassword(String password){ this.password = password; } }
service接口:IUserService.java
package com.example.springboot.service; import com.example.springboot.entity.User; import java.util.List; import java.util.Map; public interface IUserService { User get(Long id); List<User> find(Map map); boolean save(User user); boolean update(User user); int delete(Long id); }
service实现类:UserService.java
package com.example.springboot.service.impl; import com.example.springboot.dao.UserDao; import com.example.springboot.entity.User; import com.example.springboot.service.IUserService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; import java.util.Map; @Service public class UserService implements IUserService { @Resource private UserDao userDao; @Override public User get(Long id) { return userDao.get(id); } @SuppressWarnings("rawtypes") @Override public List<User> find(Map map) { return userDao.find(map); } @Override public boolean save(User user) { return userDao.save(user); } @Transactional @Override public boolean update(User user) { return userDao.update(user); } @Override public int delete(Long id) { return userDao.delete(id); } }
controller类:UserController.java
package com.example.springboot.controller; import com.example.springboot.entity.User; import com.example.springboot.service.IUserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user") @Validated public class UserController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private IUserService userService; @GetMapping("/get") public User getUser(Long id) { return userService.get(id); } }
配置文件:application.yaml
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/demo?useSSL=false&useUnicode=true&characterEncoding=utf-8 username: root password: root driver-class-name: com.mysql.jdbc.Driver mybatis: mapperLocations: classpath:mapper/*.xml typeAliasesPackage: com.example.springboot.entity
启动项目
访问:http://localhost:8080/user/get?id=1