(1)引入start(JDBC,MySQl,MyBatis)
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency>
(2)配置数据库
spring.datasource.username=root spring.datasource.password=159263487qwe spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #整合MyBatis mybatis.mapper-locations=classpath:mybatis/mapper/*.xml mybatis.type-aliases-package=com.my.pojo
(3)实体类、mapper
@Mapper @Repository public interface UserMapper { List<User> queryAll (); User queryById(int id); int add (User user); int delete (int id); int update (User user); }
(4)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.my.mapper.UserMapper"> <select id="queryAll" resultType="User"> select * from user </select> <select id="queryById" resultType="User"> select * from user where id = #{id} </select> </mapper>
(5)Controller
调用Servce层方法