注解方式
application.yml
spring: datasource: # 数据源基本配置 username: root password: root driver-class-name: com.mysql.jdbc.Driver # serverTimezone=UTC解决时区问题 url: jdbc:mysql://127.0.0.1:3306/money?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC type: com.alibaba.druid.pool.DruidDataSource # 数据源其他配置 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 # schema:执行对应路径的sql文件 # - classpath:sql/1.sql # - classpath:sql/2.sql
package feizhou.mapper; import feizhou.bean.Department; import org.apache.ibatis.annotations.*;@Mapper//指定这是一个操作数据库的mapper,或者这里不配mapper,需要主入口配置mapper的扫描包 //@MapperScan(value = "feizhou.mapper"),好处就是不需要每个mapper接口都要配置@Mapper public interface DepartmentMapper { @Select("select * from department where DepartmentID=#{id}") public Department getDeptById(String id); }
@RestController public class DeptController { @Autowired DepartmentMapper departmentMapper; @GetMapping("/dept/{id}") public Department getDepartment(@PathVariable("id") String id){ return departmentMapper.getDeptById(id); }
配置文件方式
spring: datasource: # 数据源基本配置 username: root password: root driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/money?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC type: com.alibaba.druid.pool.DruidDataSource # 数据源其他配置 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 # schema:执行对应路径的sql文件 # - classpath:sql/1.sql # - classpath:sql/2.sql #和注解方式的区别就是多了这个配置 mybatis: # 指定全局配置文件位置 config-location: classpath:mybatis/mybatis-config.xml # 指定sql映射文件位置 mapper-locations: classpath:mybatis/mapper/*.xml
UserMapper.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="feizhou.mapper.UserMapper"> <select id="getUserById" resultType="feizhou.bean.User"> SELECT * FROM userinfo WHERE UserID=#{id} </select> </mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--设置驼峰--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>
UserMapper
import feizhou.bean.User; import org.apache.ibatis.annotations.Mapper; //@Mapper或者@MapperScan将接口扫描装配到容器中 @Mapper public interface UserMapper { public User getUserById(Integer id); }
@RestController public class DeptController { // @Autowired // DepartmentMapper departmentMapper; // // // // // // @GetMapping("/dept/{id}") // public Department getDepartment(@PathVariable("id") String id){ // // return departmentMapper.getDeptById(id); // } @Autowired UserMapper userMapper; @GetMapping("/user/{id}") public User getUser(@PathVariable("id") Integer id){ return userMapper.getUserById(id); } }
测试结果