Spring Boot(六) Spring Boot整合Mybatis
在Spring Boot (二):Spring Boot Web综合开发篇中,我们已经对Mybatis做了简单的整合,此处简单复制前面的代码,作简单说明
1.添加依赖文件
xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- 引入MySQL连接的依赖包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
2.mybatis配置
yaml
#数据库配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot_test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
#mybatis放置xml文件的地方,我们配置在classpath下的mapper文件夹下
mybatis:
mapper-locations: classpath*:mapper/*.xml
3.实体类
java
public class User implements Serializable {
public User() {
}
public User(Long id,String name, String password, String mail, String date) {
this.id = id;
this.name = name;
this.password = password;
this.mail = mail;
this.date = date;
}
// ID,用户名,密码,邮箱,注册时间
private Long id;
private String name;
private String password;
private String mail;
private String date;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", mail='" + mail + '\'' +
", date='" + date + '\'' +
'}';
}
}
4.dao层
java
@Mapper
public interface UserDao{
/**
* @desc 插入
* @param user
*/
int insertUser(User user);
/**
* @desc 更新
* @param user
*/
void updateUser(User user);
/**
* @desc 删除
* @param id
*/
void delete(Long id);
/**
*
* @author cos
* @desc 返回结果的绑定
*/
List<User> findAll();
}
5.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.w3cjava.modules.user.dao.UserDao">
<sql id="testColumns">
a.id AS "id",
a.name AS "name",
a.password AS "password",
a.mail AS "mail",
a.date AS "date"
</sql>
<sql id="testJoins">
</sql>
<!-- 查询所有 -->
<select id="findAll" resultType="com.w3cjava.modules.user.entity.User">
select
<include refid="testColumns"/>
from user a
</select>
<!-- 新增 -->
<insert id="insertUser" parameterType="com.w3cjava.modules.user.entity.User">
INSERT INTO user (id,name, password,mail,date) VALUES (#{id},#{name}, #{password},#{mail},#{date})
</insert>
<!-- 更新 -->
<update id="updateUser" parameterType="com.w3cjava.modules.user.entity.User">
UPDATE user
SET name = #{name}, password = #{password},mail = #{mail},date = #{date}
WHERE id = #{id}
</update>
<!-- 删除 -->
<delete id="delete">
DELETE FROM user WHERE id =#{id}
</delete>
</mapper>
6.Junit测试
java
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
@Autowired
private UserDao userDao;
@Test
public void testInsertUser() {
User user = new User(1L,"w3cjava", "123456", "w3cjava@w3cjava.com", "2017-01-01");
userDao.insertUser(user);
}
@Test
public void testUpdateUser() {
User user = new User(1L, "w3cjava1", "1234561", "1w3cjava@w3cjava.com", "2017-01-01");
userDao.updateUser(user);
}
@Test
public void testFindAll() {
System.out.println(userDao.findAll());
}
@Test
public void testDelete() {
userDao.delete(1L);
}
@Test
public void testCURD() {
testInsertUser();
testUpdateUser();
testFindAll();
testDelete();
}
}
整合mybatis比较简单,运行Junit测试
java
@Test
public void testCURD() {
testInsertUser();
testUpdateUser();
testFindAll();
testDelete();
}