springBoot整合Mybatis,Junit
整合Mybatis
SpringBoot的版本:2.2.5.RELEASE Mybatis版本:mybatis-spring-boot-starter 2.1.2
添加Mybatis的起步依赖
<!-- Mybatis的起步依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency>
添加数据库的驱动坐标
springBoot自动管理了mysql驱动的坐标,不用自己导入版本
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
添加数据库连接信息,并使用druid连接池
# DBconfiguration: spring: datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: root url: jdbc:mysql://127.0.0.1:3306/test type: com.alibaba.druid.pool.DruidDataSource
创建User表
在test数据库中创建表
-- ---------------------------- -- 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', '李四');
创建实体Bean
package xyz.ytfs.entity; /** * @author by 雨听风说 * @Classname User * @Description TODO(用户的实体) * @Date 2020/5/13 15:45 */ public class User { private Integer id; private String username; private String password; private String name; 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 getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", name='" + name + '\'' + '}'; } }
编写Mapper
package xyz.ytfs.mapper; import xyz.ytfs.entity.User; import java.util.List; /** * @author by 雨听风说 * @Classname UserMapper * @Description TODO(用户的mapper接口) * @Date 2020/5/13 15:49 */ public interface UserMapper { List<User> findUserAll(); }
配置Mapper配置文件
<?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="xyz.ytfs.entity.User"> <select id="findUserAll" resultType="xyz.ytfs.entity.User"> select * from user; </select> </mapper>
在application.yml中 添加mybatis信息
#mybatis信息
mybatis:
#pojo别名扫描包
type-aliases-package: xyz.ytfs.entity
#加载Mybatis映射文件
mapper-locations: classpath:xyz/ytf/mapper/*Mapper.xml
编写测试的controller
package xyz.ytfs.controller; 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.ResponseBody; import xyz.ytfs.entity.User; import xyz.ytfs.mapper.UserMapper; import java.util.List; /** * @author by 雨听风说 * @Classname MybatisController * @Description TODO(mybatis 的控制层) * @Date 2020/5/13 15:58 */ @Controller public class MybatisController { @Autowired private UserMapper userMapper; @RequestMapping("query") @ResponseBody public List<User> findUserALl() { return this.userMapper.findUserAll(); } }
测试
整合Junit
添加起步依赖
<!-- springBoot整合junit测试的起起步依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>
测试
package xyz.ytfs.test; import org.junit.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.SpringRunner; import xyz.ytfs.MybatisApplication; import xyz.ytfs.mapper.UserMapper; /** * @author by 雨听风说 * @Classname JunitTest * @Description TODO(Juint整合测试) * @Date 2020/5/13 16:40 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = MybatisApplication.class) public class JunitTest { @Autowired private UserMapper userMapper; @Test public void testFindAll() { //查询并且循环输出 this.userMapper.findUserAll().forEach(System.out::println); } }
本文来自博客园,作者:BaldHead,转载请注明原文链接:https://www.cnblogs.com/strict/p/12894039.html