springboot整合mybatis
2017-08-02 21:23
507 查看
参考:catoop:http://blog.csdn.net/catoop/article/details/50553714
官方示例:https://github.com/mybatis/spring-boot-starter
基于XML方式整合mybatis
1.添加mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
2.创建一个entity包,用来存放实体类Student
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
3.创建一个mapper包,存放StudentMapper.java,里面包含一个根据ID获取Student的方法
@Mapper
public interface StudentMapper{
public Student getStudentById(int id);
}
4.由于springboot的资源文件或配置文件都在resource中,所以在resource目录下创建StudentMapper.java对应的mapper XML文件StudentMapper.xml
5.resource中创建mybatis的配置文件,添加映射
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.springboot.entity"/>
</typeAliases>
<mappers>
<mapper resource="com/springboot/mapper/StudentMapper.xml"/>
</mappers>
</configuration>然后在application.yml文件中,引用mybatis的配置文件
mybatis.config-location: classpath:mybatis-config.xml
6.单元测试
application.xml数据库配置如下:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db_test
username: root
password: root
官方示例:https://github.com/mybatis/spring-boot-starter
基于XML方式整合mybatis
1.添加mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
2.创建一个entity包,用来存放实体类Student
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
3.创建一个mapper包,存放StudentMapper.java,里面包含一个根据ID获取Student的方法
@Mapper
public interface StudentMapper{
public Student getStudentById(int id);
}
4.由于springboot的资源文件或配置文件都在resource中,所以在resource目录下创建StudentMapper.java对应的mapper XML文件StudentMapper.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.springboot.mapper.StudentMapper"> <resultMap id="studentMap" type="Student"> <id property="id" column="id" /> <result property="name" column="name" /> <result property="age" column="age" /> </resultMap> <!--id与对应的mapper.java中的方法名要一致--> <select id="getStudentById" resultType="Student" parameterType="int"> SELECT * FROM STUDENT WHERE ID = #{id} </select> </mapper>StudentMapper.xml所在包的路径应与StudentMapper.java的包路径一致
5.resource中创建mybatis的配置文件,添加映射
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.springboot.entity"/>
</typeAliases>
<mappers>
<mapper resource="com/springboot/mapper/StudentMapper.xml"/>
</mappers>
</configuration>然后在application.yml文件中,引用mybatis的配置文件
mybatis.config-location: classpath:mybatis-config.xml
6.单元测试
application.xml数据库配置如下:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db_test
username: root
password: root
@RunWith(SpringRunner.class) @SpringBootTest public class SpringbootMybatisApplicationTests { @Autowired private StudentMapper studentMapper; @Test public void contextLoads() { Student student=studentMapper.getStudentById(3); System.out.println(student.getName()); } }
相关文章推荐
- Spring boot+ Mybatis 完美整合之注解版
- Spring boot Mybatis 整合(完整版)
- Springboot整合mybatis及分页查询、定时任务)
- SpringBoot整合Mybatis,简单明了
- springboot mybatis整合
- Spring boot Mybatis 整合(完整版)
- Spring boot整合mybatis
- Spring Boot系列之六 以注解方式整合MyBatis
- Spring boot Mybatis 整合(完整版)
- spring boot整合mybatis+mybatis-plus
- SpringBoot04 SpringBoot 和 MyBatis 整合
- spring_boot整合mybatis项目启动时报错
- SpringBoot整合常用技术--MyBatis、Redis、定时任务、分页插件、逆向生成
- Springboot整合Mybatis实现Druid多数据源
- SpringBoot整合MyBatis
- springboot学习笔记-2 一些常用的配置以及整合mybatis
- spring boot和mybatis的整合
- Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
- Spring Boot之整合Mybatis
- Spring Boot、Mybatis框架整合开发Java RESTful Web Service