1. pom.xml
具体pom.xml文件参考blogger源码
其中h2的scope能够是test。
2. springboot应用mybatis进行数据库操作
应用mybatis时,须要org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.3[compile]
依赖。
而后创立一个接口,应用@Mapper
标记,mybatis会搜寻此标记,把这个接口解析为数据库操作接口。
package com.ws.product.blogger.dao;import com.ws.product.blogger.dao.pojo.User;import org.apache.ibatis.annotations.Mapper;import org.springframework.stereotype.Component;@Mapper // mybatis用于发现mapper接口@Component // 能够省略。次要是为了在IDEA里UserDaoTest中@Autowired时不报错public interface UserDao { User getById(int id); User getByUsername(String username); // 应用java bean传递多个参数,在xml中能够间接应用bean的property name,即#{username} int insert(User user); int delete(int id); // 两个参数,第二个参数是java bean,应用的时候须要应用#{user.username} int update(int id, User user);}
User
是一个POJO,用来保留数据库中的数据:
package com.ws.product.blogger.dao.pojo;import lombok.Data;@Datapublic class User { private int id; private String username; private String password;}
而后在src/main/resources目录下新建mapper目录,再新建UserDao.xml文件,具体的SQL语句就写在这里。这个文件在哪里无所谓,之后通过application.yml配置文件中的mybatis.mapper-locations: classpath:mapper/*.xml
属性找到这个文件。
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.ws.product.blogger.dao.UserDao"><!--namespace是java interface和xml的匹配关系--> <resultMap id="UserMap" type="com.ws.product.blogger.dao.pojo.User"> <id column="id" property="id" jdbcType="INTEGER"/> <result column="username" property="username" jdbcType="VARCHAR"/> <result column="password" property="password" jdbcType="VARCHAR"/> </resultMap> <select id="getById" resultMap="UserMap"> select * from t_user where id = #{id} </select> <select id="getByUsername" resultMap="UserMap"> select * from t_user where username = #{username} </select> <insert id="insert" parameterType="com.ws.product.blogger.dao.pojo.User"> insert into t_user (username, password) values ( #{username}, #{password} ) </insert> <delete id="delete"> delete from t_user where id = #{id} </delete> <update id="update"> update t_user <set> <if test="user.username != null">username = #{user.username},</if> <if test="user.password != null">password = #{user.password}</if> </set> where id = #{id} </update></mapper>
这样,mybatis就具备了操作数据库的能力。对于独自一个DAO模块来说,这曾经足够了。如果想要真正执行数据库操作,须要新建一个main函数,应用@SpringBootApplication标记,application.yml中配置spring.datasource.url, username, password, driver-class-name属性,这会主动生成一个sqlSession,mybatis会应用这个sqlSession操作数据库。不要忘了,application.yml中还须要配置mybatis.mapper-locations属性。
3. 测试mybatis
咱们应用org.mybatis.spring.boot:mybatis-spring-boot-starter-test:2.1.3[test]
对mybatis进行测试,应用H2数据库的内存数据库,使得数据库测试能够反复执行。
首先创立一个空的@SpringBootApplication标记的类,用以加载所有应用到的bean:
package com.ws.product.blogger.dao;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * 这是一个哨兵类,须要增加@SpringBootApplication注解,让@MybatisTest能够主动加载所需的类。必不可少 */@SpringBootApplicationpublic class MapperTestApplication {}
而后创立测试类,应用@MybatisTest
标记:
package com.ws.product.blogger.dao;import com.ws.product.blogger.dao.pojo.User;import org.junit.jupiter.api.Test;import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;import org.springframework.test.context.ActiveProfiles;import static org.junit.jupiter.api.Assertions.assertEquals;@MybatisTest@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) // 应用实在的数据库,这里咱们应用H2内存数据库// @Rollback(value = false) // 默认为true,数据库操作会回滚。改为false后,不会回滚@ActiveProfiles("test") // 应用appplication-test.yml作为配置文件public class UserDaoTest { @Autowired private UserDao userDao; @Test public void test01() { User user = new User(); user.setUsername("aaa"); user.setPassword("aaa"); int i = userDao.insert(user); assertEquals(1, i); } @Test public void test02() { User user = userDao.getByUsername("root"); assertEquals(1, user.getId()); assertEquals("root", user.getUsername()); assertEquals("rootqqq", user.getPassword()); } @Test public void test03() { User user = userDao.getById(1); assertEquals(1, user.getId()); assertEquals("root", user.getUsername()); assertEquals("rootqqq", user.getPassword()); } @Test public void test04() { int i = userDao.delete(1); assertEquals(1, i); } @Test public void test05() { User user = new User(); // user.setUsername("root"); user.setPassword("root2"); userDao.update(1, user); User user1 = userDao.getById(1); assertEquals("root", user1.getUsername()); assertEquals("root2", user1.getPassword()); }}
创立配置文件src/test/resources/application-test.yml
。spring.datasource.shcema和data是建表语句和铺底数据语句
spring: datasource: url: jdbc:h2:mem:testdb username: sa password: sa driver-class-name: org.h2.Driver schema: classpath:sql/db/schema-h2.sql data: classpath:sql/db/data-h2.sqlmybatis: mapper-locations: classpath:mapper/*.xml
schema-h2.sql中内容为:
drop table if exists t_user;create table t_user( id integer not null auto_increment, username varchar(100) not null, password varchar(200) not null, primary key (id));
data-h2.sql中内容为:
insert into t_user values(1, 'root', 'rootqqq');
测试过程为:启动UserDaoTest
,依据@MybatisTest
找到@SpringBootApplication
标记ide类,加载所有bean,加载application-test.yml。依据spring.datasource的配置创立H2内存数据库,并加载schema-h2.sql和data-h2.sql创立表和数。而后进行mybatis操作,生成UserDao实例,主动注入到UserDaoTest中,进行测试。
参考文档
- Mybatis传递多个参数的4种形式
- MyBatis 传递多个参数
- mybatis中文教程
- 动静 SQL