一、pom文件引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
二、application文件配置属性
.yml 文件
# mapper-locations:mybatis扫描的xml文件路径 #type-aliases-package:mybatis扫描的实体类路径 mybatis: mapper-locations: classpath:mappers/*xml type-aliases-package: com.yunluo.appointment.*.model server: port: 8080 spring: application: name: app datasource: driver-class-name: com.mysql.cj.jdbc.Driver name: defaultDataSource password: 123489 url: jdbc:mysql://localhost:3306/da?serverTimezone=UTC username: su
.properties 文件
mybatis.mapper-locations=classpath:mappers/*.xml mybatis.type-aliases-package=com/example/times/user/model server.port=8080 spring.application.name=appointment spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.name=defaultDataSource spring.datasource.url=jdbc:mysql://localhost:3306/dda?serverTimezone=UTC spring.datasource.username= sun spring.datasource.password=1234
三、启动类添加注解@MapperScan
package com.yunluo.appointment; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.yunluo.appointment.*.mapper") public class AppointmentApplication { public static void main(String[] args) { SpringApplication.run(AppointmentApplication.class, args); } }
四、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.yunluo.appointment.user.mapper.UserMapper"> <resultMap id="map" type="com.yunluo.appointment.user.model.User"> <id column="id" property="id" jdbcType="java.lang.Integer"/> <result column="name" property="name" jdbcType="java.lang.String"/> <result column="passward" property="passward"jdbcType="java.lang.String"/> </resultMap> <sql id="Base_Column_List"> id, name, passward </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="map"> select <include refid="Base_Column_List"/> from t_user where id = #{id,jdbcType=INTEGER} </select> </mapper>
五 、定义实体类
package com.yunluo.appointment.user.model; import lombok.ToString; @ToString public class User { private Integer id; private String name; private String passward; public User(Integer id, String name, String passward) { this.id = id; this.name = name; this.passward = passward; } public User() { super(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassward() { return passward; } public void setPassward(String passward) { this.passward = passward; } }
六、定义Service接口以及其实现类
package com.yunluo.appointment.user.service; import com.yunluo.appointment.user.model.User; import org.springframework.transaction.annotation.Transactional; @Transactional public interface UserService { User selectByPrimaryKey(Integer id); }
package com.yunluo.appointment.user.service.imp; import com.yunluo.appointment.user.mapper.UserMapper; import com.yunluo.appointment.user.model.User; import com.yunluo.appointment.user.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User selectByPrimaryKey(Integer id) { return userMapper.selectByPrimaryKey(id); } }
七、定义Junit测试类
package com.yunluo.appointment; import com.yunluo.appointment.user.model.User; import com.yunluo.appointment.user.service.UserService; import org.junit.Before; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = AppointmentApplication.class) class AppointmentApplicationTests { @Test void contextLoads() { } @Autowired private UserService userService; private User user; @Before public void setUp() throws Exception { user = new User(); } @Test public void get() { user = userService.selectByPrimaryKey(1); System.out.println(user); } }
八、定义Controller类
package com.yunluo.appointment.user.controller; import com.yunluo.appointment.user.model.User; import com.yunluo.appointment.user.service.UserService; import com.yunluo.appointment.util.JsonData; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; private User user; @RequestMapping("/get") public JsonData get(Integer id) { user = userService.selectByPrimaryKey(id); JsonData jsonData = new JsonData(); jsonData.setResult(user); return jsonData; } }