Springboot整合Mybatis
程序员文章站
2024-01-26 12:44:52
...
1:项目目录结构
2 :pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>com.springbmybatis</groupId>
<artifactId>MySpringBootMyBatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MySpringBootMyBatis</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
<version>2.9.6</version><!--$NO-MVN-MAN-VER$-->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3:application.yml配置
server:
port: 8080
context-path: /springbootMybatis
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8
username: root
password: mysql
mybatis:
mapperLocations: classpath:/mapper/UserMapper.xml
typeAliasesPackage: com.springbmybatis.entity
4:UserMapper.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.springbmybatis.mapper.UserMapper">
<resultMap type="com.springbmybatis.entity.User" id="UserMap" autoMapping="true">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="password" property="password"/>
<result column="sex" property="sex"/>
<result column="job" property="job"/>
<result column="birthday" property="birthday"/>
<result column="salary" property="salary"/>
</resultMap>
<!-- 添加用户 -->
<insert id="addUser" parameterType="User">
insert into t_user
(id,birthday,job,name,password,salary,sex)
values (#{id},#{birthday},#{job},#{name},#{password},#{salary},#{sex})
</insert>
<!-- 根据id查询用户 -->
<select id="findUserById" parameterType="java.lang.Integer" resultMap="UserMap">
SELECT * FROM t_user WHERE id=#{id}
</select>
<!-- 用户信息列表 -->
<select id="listUser" resultType="com.springbmybatis.entity.User">
SELECT * FROM t_user
</select>
</mapper>
5:启动类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.springbmybatis.mapper")
public class MySpringBootMyBatisApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootMyBatisApplication.class, args);
}
}
6:controller
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.springbmybatis.entity.User;
import com.springbmybatis.service.UserService;
@RestController
public class UserController {
@Resource
private UserService userService;
@GetMapping(value="/findUserById/{id}")
public User findUserById(@PathVariable("id") Integer userId) {
return userService.findUserById(userId);
}
@GetMapping(value="/addUser")
public void addUser(User user) {
userService.addUser(user);
}
@GetMapping(value="/findAllUser")
public List<User> findAllUser(){
return userService.findAllUser();
}
}
7:service
import java.util.List;
import com.springbmybatis.entity.User;
public interface UserService {
public void addUser(User user);
public User findUserById(Integer id);
public List<User> findAllUser();
}
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.springbmybatis.entity.User;
import com.springbmybatis.mapper.UserMapper;
import com.springbmybatis.service.UserService;
@Service
public class UserServiceImple implements UserService{
@Resource
private UserMapper userMapper;
@Override
public void addUser(User user) {
userMapper.addUser(user);
}
@Override
public User findUserById(Integer id) {
return userMapper.findUserById(id);
}
@Override
public List<User> findAllUser() {
return userMapper.listUser();
}
}
8:Mapper
import java.util.List;
import com.springbmybatis.entity.User;
public interface UserMapper {
public void addUser(User user);
public User findUserById(Integer id);
public List<User> listUser();
}
9:实体类
public class User implements Serializable{
private static final long serialVersionUID = -5105237253493472875L;
private Integer id;
private String name;
private String password;
private String sex;
private String job;
private String birthday;
private Double salary;
public User() {
super();
}
public User(Integer id) {
super();
this.id = id;
}
public User(Integer id, String name, String password, String sex,
String job, String birthday, Double salary) {
super();
this.id = id;
this.name = name;
this.password = password;
this.sex = sex;
this.job = job;
this.birthday = birthday;
this.salary = salary;
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}
10:表
CREATE TABLE `t_user` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` char(20) DEFAULT NULL,
`password` char(20) DEFAULT NULL,
`sex` char(6) DEFAULT NULL,
`job` char(20) DEFAULT NULL,
`birthday` char(20) DEFAULT NULL,
`salary` char(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
11:启动访问
http://127.0.0.1:8080/springbootMybatis/findUserById/1 --查询单条
http://127.0.0.1:8080/springbootMybatis/findAllUser --查询全部
http://127.0.0.1:8080/springbootMybatis/addUser?name=lisi&password=lisis&sex=nan&job=11 --增加