mybatis开发团队为Spring Boot 提供了 MyBatis-Spring-Boot-Starter。所以如果觉的使用springdata不好用,或者是习惯使用mybatis,可以将springboot与mybatis进行整合。
1.添加依赖
pom.xml
<!-- 使用mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
2.创建实例bean
UserInfo
package com.tang.bean;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import com.alibaba.fastjson.annotation.JSONField;
@Entity
@Table(name="springboot_userinfo")
public class UserInfo {
@Id
@GeneratedValue
private Integer id;
private String userName;
private String loginName;
//不返回json
@JSONField(serialize=false)
private String password;
private Integer age;
@JSONField(format="yyyy-MM-dd HH:mm")
private Date birthDate;
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 getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
@Override
public String toString() {
return "UserInfo [id=" + id + ", userName=" + userName + ", loginName="
+ loginName + ", password=" + password + ", age=" + age
+ ", birthDate=" + birthDate + "]";
}
}
3.创建mapper接口
为mybatis定义接口方法
UserInfoMybatisMapper
package com.tang.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import com.tang.bean.UserInfo;
/**
* 添加mybatis的@Mapper注解,将接口放入ioc容器,@Repository不起作用
*/
//@Repository("userInfoMybatisMapper")
@Mapper
public interface UserInfoMybatisMapper {
public List<UserInfo> getAllUserInfos();
public UserInfo getUserInfoById(@Param("uid") Integer id);
}
4.创建service
UserInfoMybatisService
package com.tang.service;
import java.util.List;
import com.tang.bean.UserInfo;
public interface UserInfoMybatisService {
public List<UserInfo> getUserInfosByMybatis();
public UserInfo getUserInfosByMybatisById(Integer id);
}
UserInfoMybatisServiceImpl
package com.tang.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tang.bean.UserInfo;
import com.tang.mapper.UserInfoMybatisMapper;
import com.tang.service.UserInfoMybatisService;
@Service("userInfoMybatisService")
public class UserInfoMybatisServiceImpl implements UserInfoMybatisService{
@Autowired
public UserInfoMybatisMapper userInfoMybatisMapper;
public List<UserInfo> getUserInfosByMybatis() {
List<UserInfo> allUserInfos = userInfoMybatisMapper.getAllUserInfos();
return allUserInfos;
}
public UserInfo getUserInfosByMybatisById(Integer id) {
return userInfoMybatisMapper.getUserInfoById(id);
}
}
5.创建controller
UserInfoMybatisController
package com.tang.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tang.bean.UserInfo;
import com.tang.service.UserInfoMybatisService;
@RestController
public class UserInfoMybatisController {
@Autowired
private UserInfoMybatisService userInfoMybatisService;
@RequestMapping(value="/mybatisGetAllUserInfos")
public List<UserInfo> getAllUserInfos(){
return userInfoMybatisService.getUserInfosByMybatis();
}
@RequestMapping(value="/mybatisGetUserInfoById/{uid}")
public UserInfo getUserInfoById(@PathVariable("uid") Integer id){
return userInfoMybatisService.getUserInfosByMybatisById(id);
}
}
6.创建mapper配置文件
userinfomapper.xml
目录src/main/resources/mybatis/mapper
提供两个方法,返回结果分别使用resultMap和resultType封装
<?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.tang.mapper.UserInfoMybatisMapper">
<sql id="userinfo_field" >
id, user_name, login_name,password,age,birth_date
</sql>
<select id="getAllUserInfos" resultMap="allUserInfos">
select
<include refid="userinfo_field"/>
from springboot_userinfo
</select>
<resultMap type="com.tang.bean.UserInfo" id="allUserInfos">
<id column="id" property="id"/>
<result column="user_name" property="userName"/>
<result column="login_name" property="loginName"/>
<result column="password" property="password"/>
<result column="age" property="age"/>
<result column="birth_date" property="birthDate"/>
</resultMap>
<select id="getUserInfoById" resultType="com.tang.bean.UserInfo">
select
<include refid="userinfo_field"/>
from springboot_userinfo
where id = #{uid}
</select>
</mapper>
7.创建主配置文件
mybatis-config.xml
目录src/main/resources/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>
<!--
<properties resource="db.properties"></properties>
-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
8.添加mybatis在application.properties文件的配置
#mybatis配置
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
9.修改启动类
在这里需要指定定义的mybatis接口在哪个包下面,以便获取mapper接口,放入到ioc容器中
package com.tang;
import java.util.List;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication
@MapperScan(value="com.tang.mapper.*")//扫描mybatis包下相应的class,主要是mybatis的持久化类
public class App{
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
11.测试
http://localhost:8080/springboot/mybatisGetUserInfoById/1获取Id为1的userInfo
http://localhost:8080/springboot/mybatisGetAllUserInfos获取所有的userInfo
以上就是集成基于配置文件的mybatis详细步骤,当然也可以基于注解进行集成。