1 SpringBoot整合Mybatis
1.1 添加Mybatis的起步依赖
<!--mybatis起步依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency>
1.2 添加数据库驱动坐标
<!-- MySQL连接驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
1.3 添加数据库连接信息
在application.yml中添加数据量的连接信息
#DB Configuration: spring: #连接池配置 datasource: type: com.alibaba.druid.pool.DruidDataSource druid: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/webdatabase?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: 123 initial-size: 1 #连接池初始大小 max-active: 20 #连接池中最大的活跃连接数 min-idle: 1 #连接池中最小的活跃连接数 max-wait: 60000 #配置获取连接等待超时的时间 pool-prepared-statements: true #打开PSCache,并且指定每个连接上PSCache的大小 max-pool-prepared-statement-per-connection-size: 20 validation-query: SELECT 1 FROM DUAL validation-query-timeout: 30000 test-on-borrow: false #是否在获得连接后检测其可用性 test-on-return: false #是否在连接放回连接池后检测其可用性 test-while-idle: true #是否在连接空闲一段时间后检测其可用性
1.4 创建user表
在test数据库中创建user表
-- ---------------------------- -- Table structure for `user` -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) DEFAULT NULL, `password` varchar(50) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张三'); INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');
1.5 创建实体Bean
@Data @AllArgsConstructor @NoArgsConstructor public class User { // 主键 private Integer id; // 用户名 private String username; // 密码 private String password; // 姓名 private String name; }
1.6 编写Mapper
@Mapper public interface UserMapper { public List<User> queryUserList(); }
注意:@Mapper标记该类是一个mybatis的mapper接口,可以被spring boot自动扫描到spring上下文中
@MapperScan("com.fs.boot.mapper") 在主启动类上使用此注解来指定扫描的mapper接口层
1.7 配置Mapper映射文件
在src\main\resources\mapper路径下加入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="com.fs.boot.mapper.UserMapper"> <select id="queryUserList" resultType="user"> select * from user </select> </mapper>
1.8 在application.yml中添加mybatis的信息
#spring集成Mybatis环境 #pojo别名扫描包 #加载Mybatis映射文件 mybatis: mapper-locations: classpath:config/mapper/*Mapper.xml type-aliases-package: com.fs.boot.pojo configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
1.9 编写服务层和控制器Controller
@Controller public class MapperController { @Autowired private UserService userService; @RequestMapping("/queryUser") @ResponseBody public List<User> queryUser(){ List<User> users = userService.queryUserList(); return users; } }
1.10 测试
1.11 编写BaseMapper
通过UserMapper 继承该接口后,无需编写每个 mapper.xml 文件中的CURD功能方法,即可获得CRUD功能
/** * Mapper父接口 * @param <T>泛型 */ public interface BaseMapper<T> { /** * 插入一条记录 * @param entity * 实体对象 * @return int */ Integer insert(T entity) throws Exception; /** * 根据 ID 修改 * @param entity * 实体对象 * @return int */ Integer update(T entity)throws Exception; /** * 根据 ID 删除 * @param id * 主键ID * @return int */ Integer deleteById(Serializable id) throws Exception; /** * 根据 ID 查询 * @param id * 主键ID * @return T */ T selectById(Serializable id) throws Exception; /** * 查询所有 * @return List<T> */ List<T> selectAll() throws Exception; } @Repository public interface UserMapper extends BaseMapper<User> { User selectByName(String username); }标签:mapper,return,SpringBoot,spring,技术,id,user,整合,public From: https://blog.csdn.net/ysbwjby/article/details/142043242