SpringBoot整合Mybatis
参考 https://blog.csdn.net/iku5200/article/details/82856621
1、新建springboot项目

2、修改配置文件:application-dev.yml
server: port: 8080 spring: datasource: username: root password: 1234 url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:mapping/*Mapper.xml type-aliases-package: com.example.entity #showSql logging: level: com: example: mapper : debug
3、创建实体类,实现业务流程
3.1在数据库中创建表
CREATE TABLE `user` ( `id` int(32) NOT NULL AUTO_INCREMENT, `userName` varchar(32) NOT NULL, `passWord` varchar(50) NOT NULL, `realName` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
3.2创建实体(推荐用lombok的注解,可以省略geterseter等方法)
package com.example.entity; public class User { private Integer id; private String userName; private String passWord; private String realName; public Integer getId() { return id; } public void setId(Integer 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; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } @Override public String toString() { return "User{" + "id=" + id + ", userName='" + userName + '\'' + ", passWord='" + passWord + '\'' + ", realName='" + realName + '\'' + '}'; } }
4、controller.java(注意区分@Controller和@RestController注解)
package com.example.controller; import com.example.entity.User; import com.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/testBoot") public class UserController { @Autowired private UserService userService; @RequestMapping("getUser/{id}") public String GetUser(@PathVariable int id){ return userService.Sel(id).toString(); } }
5、service.java(@Service注解)
package com.example.service; import com.example.entity.User; import com.example.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired UserMapper userMapper; public User Sel(int id){ return userMapper.Sel(id); } }
6、mapper
6.1 mapper.java
package com.example.mapper; import com.example.entity.User; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; @Repository public interface UserMapper { User Sel(int id); }
6.2 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.example.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.entity.User"> <result column="id" jdbcType="INTEGER" property="id" /> <result column="userName" jdbcType="VARCHAR" property="userName" /> <result column="passWord" jdbcType="VARCHAR" property="passWord" /> <result column="realName" jdbcType="VARCHAR" property="realName" /> </resultMap> <select id="Sel" resultType="com.example.entity.User"> select * from user where id = #{id} </select> </mapper>
最终架构

7、完成以上,在启动类中加上注解,用于扫描mapper的文件路径@MapperScan("com.example.mapper")
package com.example; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.example.mapper") //扫描的mapper @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
浙公网安备 33010602011771号