<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>xatu.zslgroupId>
<artifactId>spring_boot_mybatisartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>spring_boot_mybatisname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.6.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.40version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.0.5version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
(2). 添加 application.yml(yml比起 properties文件看起来清爽很多,简介很多)
spring:
datasource:
name: spring_boot_mybatis
url: jdbc:mysql://localhost:3306/mybatis_demo1
username: root
password: 1486145487
# type: com.alibaba.druid.pool.DruidDataSource # 使用druid 数据源
driver-class-name: com.mysql.jdbc.Driver
# dbcp2:
# min-idle: 1
# max-idle: 2
# initial-size: 1
# time-between-eviction-runs-millis: 3000
# min-evictable-idle-time-millis: 300000
# validation-query: SELECT "ZTM" FROM DUAL
# test-while-idle: true
# test-on-borrow: false
# test-on-return: false
mybatis:
mapper-locations: classpath:mapper/UserMapper.xml,classpath:mapper/StudentMapper.xml
type-aliases-package: xatu.zsl.entity
注意:注释地方是配置数据库连接池的东西(不配置也可以,,测试感觉不出来)
(3). 配置Mybatis 的mapper文件,,这个文件是mybatis映射的核心文件是必须的。
在这里扩展一下,放两个mapper文件,内容一样只是为了说明,springBoot整合Mybatis如何配置多个mapper文件
StudentMapper.xml
<mapper namespace="xatu.zsl.mapper.StudentMapper" >
<insert id="save" parameterType="xatu.zsl.entity.User">
insert into t_user(username,age) values(#{userName,jdbcType=VARCHAR},#{age,jdbcType=NUMERIC})
insert>
<select id="selectById" resultType="xatu.zsl.entity.User">
select * from t_user where id = #{id,jdbcType=NUMERIC}
select>
<update id="updateById" parameterType="xatu.zsl.entity.User">
update t_user set
username = #{userName,jdbcType=VARCHAR} ,
age = #{age,jdbcType=NUMERIC}
where id = #{id,jdbcType=NUMERIC}
update>
<delete id="deleteById">
delete from t_user where id = #{id,jdbcType=NUMERIC}
delete>
<select id="queryAll" resultType="xatu.zsl.entity.User">
select * from t_user
select>
mapper>
(4). 设置接口 StudentMapper
package xatu.zsl.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import xatu.zsl.entity.User;
import java.util.List;
/**
* Created by zsl on 2017/8/6.
*/
@Component
@Mapper
public interface StudentMapper {
int save(User user);
User selectById(Integer id);
int updateById(User user);
int deleteById(Integer id);
List queryAll();
}
(5). 编写实体类 User(为了方便就使用两个一样的mapper,能说明问题咋简单砸来)
package xatu.zsl.entity;
import java.io.Serializable;
/**
* Created by zsl on 2017/8/6.
*/
public class User implements Serializable {
private static final long serialVersionUID = 8809101560720973267L;
private Integer id;
private String userName;
private Integer age;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", age=" + age + "]";
}
}
(6). 启动文件不用变,,使用test 文件测试
package xatu.zsl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
//@RunWith(SpringRunner.class)
//@SpringBootTest
//public class SpringBootMybatisApplicationTests {
//
// @Test
// public void contextLoads() {
// }
//
//}
import org.springframework.beans.factory.annotation.Autowired;
import xatu.zsl.entity.User;
import xatu.zsl.mapper.StudentMapper;
import xatu.zsl.mapper.UserMapper;
import xatu.zsl.service.UserService;
@RunWith(SpringRunner.class)
@SpringBootTest()
//相当于 --spring.profiles.active=dev
//@ActiveProfiles(value="dev")
public class SpringBootMybatisApplicationTests {
@Autowired
private UserMapper mapper;
@Autowired
private StudentMapper studentMapper;
@Test
public void testInsert() {
User user = new User();
user.setUserName("张飞");
user.setAge(50);
mapper.save(user);
System.out.println("插入用户信息" + user.getUserName());
}
@Test
public void testSelect() {
User user = mapper.selectById(1);
System.out.println("查找用户成功:" + user);
}
@Test
public void testUpdate() {
User user = mapper.selectById(1);
System.out.println("修改用户信息初:" + user);
user.setAge(24);
mapper.updateById(user);
user = mapper.selectById(1);
System.out.println("修改用户信息末:" + user);
}
@Autowired
private UserService userService;
@Test
public void testTransactional() {
System.out.println("测试事务处理");
//测试事务处理
try {
userService.insetUser();
} catch (Exception e) {
e.printStackTrace();
System.out.println("事务处理异常");
}
}
@Test
public void testSelectStudent() {
User user = studentMapper.selectById(1);
System.out.println("studentMapper查找用户成功:" + user);
}
}
测试运行截图:
(2). springBoot整合Mybatis如何配置多个mapper文件,需要在applaction.yml中配置:
mybatis:
mapper-locations: classpath:mapper/UserMapper.xml,classpath:mapper/StudentMapper.xml
(3). @Mapper 注解是springBoot整合Mybatis独有的,,mybatis木有。
import org.apache.ibatis.annotations.Mapper;