用小demo来进行介绍此次的操作。
pom.xml中的依赖,以及资源文件的加载xml等配置
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
provided
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
src/main/java
**/*.xml
配置application.properties的内容如下列所示
spring.datasource.url=jdbc:mysql://localhost:3306/chapter5 #数据库连接
spring.datasource.username=root #账号
spring.datasource.password=123456 #密码
spring.datasource.tomcat.max-idle=10
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.initial-size=5
#mybatis映射文件配置
mybatis.mapper-locations=classpath:com/springboot/chapter5/mapper/*.xml
#Mybatis 扫描别名,和注解Alias联用
mybatis.type-aliases-package=com.springboot.chapter5.pojo
#配置typehandler的扫描包
mybatis.type-handlers-package=com.springboot.chapter5.typehandler
#日志配置
logging.level.root=DEBUG
logging.level.org.springframework=DEBUG
logging.level.org.org.mybatis=DEBUG
编写User类,该类与数据库中表user_table类型是相互对应的。
@Alias(value = “user”) 即为了使其xml文件可以识别出来。
package com.springboot.chapter5.pojo;
import org.apache.ibatis.type.Alias;
@Alias(value = "user") //mabatis指定别名
public class User {
private Long id = null;
private String userName = null;
private String note = null;
private String sex = null;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
dao下的接口,分别对表格增删改查。
package com.springboot.chapter5.dao;
import com.springboot.chapter5.pojo.User;
public interface MyBatisUserDao {
public User getUser(Long id);
public void insertUser(User user);
public void updateUser(User user);
public void deleteUser(Long id);
}
mapper下的映射xml文件,将接口MyBatisUserDao需要的操作映射到xml文件中,进行操作。
dao与mapper在同一级别
insert into user_table (id,user_Name,sex,note) value (#{id}, #{userName}, #{sex},#{note})
update user_table set user_Name = #{userName},sex = #{sex},note = #{note} where id = #{id}
delete from user_table where id = #{id}
该接口即为业务需要实现的接口,下面会进行描述
package com.springboot.chapter5.service;
import com.springboot.chapter5.pojo.User;
public interface MyBatisUserService {
public User getUser(Long id);
public void insertUser(User user);
public void updateUser(User user);
public void deleteUser(Long id);
}
实现上述接口MyBatisUserService,并进行业务操作,通过MyBatisUserDao,对数据库进行操作,从而进行映射。
package com.springboot.chapter5.service.impl;
import com.springboot.chapter5.dao.MyBatisUserDao;
import com.springboot.chapter5.pojo.User;
import com.springboot.chapter5.service.MyBatisUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MyBatisUserServiceImpl implements MyBatisUserService {
@Autowired
private MyBatisUserDao myBatisUserDao = null; //mybatis映射的接口
@Override
public User getUser(Long id) {
return myBatisUserDao.getUser(id);
}
@Override
public void insertUser(User user) {
myBatisUserDao.insertUser(user);
}
@Override
public void updateUser(User user) {
myBatisUserDao.updateUser(user);
}
@Override
public void deleteUser(Long id) {
myBatisUserDao.deleteUser(id);
}
}
实现服务MyBatisUserService,并根据路径实现相应的操作。
package com.springboot.chapter5.controller;
import com.springboot.chapter5.pojo.User;
import com.springboot.chapter5.service.MyBatisUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/mybatis")
public class MyBatisController {
@Autowired
private MyBatisUserService myBatisUserService = null;//连接 数据库的接口,会实例化其实现的类
@GetMapping("/getUser")
@ResponseBody
public User getUser(Long id){
return myBatisUserService.getUser(id);
}
@RequestMapping("/insertUser")
@ResponseBody
public void insertUser(User user){
myBatisUserService.insertUser(user);
}
@RequestMapping("/updateUser")
@ResponseBody
public void updateUser(User user){
myBatisUserService.updateUser(user);
}
@RequestMapping("/deleteUser")
@ResponseBody
public void deleteUser(Long id){
myBatisUserService.deleteUser(id);
}
}
这里使用了SqlSessionFactory是spring boot自动为我们生成的,然后直接使用MapperFactoryBean来定义MyBatisUserDao接口。
package com.springboot.chapter5;
import com.springboot.chapter5.dao.MyBatisUserDao;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication(scanBasePackages = {"com.springboot.chapter5"})
public class Chapter5Application {
@Autowired
SqlSessionFactory sqlSessionFactory = null;
//定义一个mabatis的mapper接口
@Bean
public MapperFactoryBean initMyBatisUserDao(){
MapperFactoryBean bean = new MapperFactoryBean<>();
bean.setMapperInterface(MyBatisUserDao.class);
bean.setSqlSessionFactory(sqlSessionFactory);
return bean;
}
public static void main(String[] args) {
SpringApplication.run(Chapter5Application.class, args);
}
}
通过链接