SpringBoot与Mybatis集成
在pom文件中引入相应的依赖
<!--引入mybatis依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
在application.yml中加入相应的配置
spring:
thymeleaf:
cache: false
datasource:
name: test
url: jdbc:mysql://localhost:3306/mybatis
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml #使用的Mapper文件位置
typeAliasesPackage: com.liusl.model #它一般对应我们的实体类所在的包,这个时候会自动简单类名作为包括包名的别名取对应包中不包括包名的别名
以一个User.java举例
package com.liusl.model;
/**
* created by l1 on 2017/12/21.
*/
public class User {
private Integer id;
private String name;
private String sex;
private Integer age;
public User(){
super();
}
public User(String name,String sex,Integer age){
super();
this.name = name;
this.sex = sex;
this.age = 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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
创建UserMapper.xml(持久化类和数据库表的配置文件)
在application.yml文件中配置了文件的存储位置mapper/
向数据库中插入数据
namespace唯一,命名规则包名+xml文件去后缀(.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.liusl.mapper.UserMapper">
<insert id="create" parameterType="com.liusl.model.User" useGeneratedKeys="true">
INSERT INTO TB_USER(name,sex,age) VALUES (#{name},#{sex},#{age})
</insert>
</mapper>
编写mapper接口(与UserMapper.xml对应)
传的参数是User类型
package com.liusl.mapper;
import com.liusl.model.User;
import org.apache.ibatis.annotations.Mapper;
/**
* created by l1 on 2017/12/21.
*/
@Mapper
public interface UserMapper {
public void create(User user);
}
编写service接口
package com.liusl.service;
import com.liusl.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* created by l1 on 2017/12/21.
*/
@Service
public interface UserService {
public void create(User user);
}
实现该接口
package com.liusl.service;
import com.liusl.mapper.UserMapper;
import com.liusl.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* created by l1 on 2017/12/21.
*/
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public void create(User user){
userMapper.create(user);
}
}
7、编写controller类
package com.liusl.controller;
import com.liusl.model.User;
import com.liusl.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* created by l1 on 2017/12/21.
*/
@RestController
@RequestMapping(value = "/user")
public class UserController {
// private User user;//接受前台传的参数实体类
@Autowired
private UserServiceImpl userService;
@RequestMapping(method = RequestMethod.POST)
public Map create(@RequestParam("name") String name,
@RequestParam("sex") String sex,
@RequestParam("age") Integer age){
User user = new User(name,sex,age);
userService.create(user);
Map response = new HashMap<String,String>();
response.put("response","success");
return response;
}
}
8、启动服务,测试