Springboot 整合mybatis
1.pom文件中导入相关依赖
- mybatis-spring-boot-starter
- mysql-connector-java
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.song</groupId>
<artifactId>springboot06</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot06</name>
<description>Demo project for Spring Boot</description>
<properties>
<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-web</artifactId>
</dependency>
<!-- 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>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.yml文件中
spring:
#设置数据源 默认hikari
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&character=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.song.pojo #配置别名
mapper-locations: classpath:mappers/*.xml #扫描mapper.xml文件
configuration:
map-underscore-to-camel-case: true #开启匹配驼峰命名
logging: #打印日志
level:
com.song.mapper: debug
pattern:
dateformat: HH:mm:ss
3.pojo实体类
package com.song.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int userId;
private String userName;
private String userPwd;
}
4.mapper层
package com.song.mapper;
import com.song.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
//查询所有用户
// @Select("select * from user")
List<User> queryAllUser();
//通过Id查询用户
User queryUserById(@Param("userId") int userId);
//新增用户
int saveUser(User user);
//修改用户
int updateUser(User user);
}
mapper.xml 对应yml中
mapper-locations: classpath:mappers/*.xml #扫描mapper.xml文件在类路径下创建
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.song.mapper.UserMapper">
<!-- 结果映射-->
<!-- <resultMap id="User" type="User">-->
<!-- <result property="userId" column="user_id"></result>-->
<!-- <result property="userName" column="user_name"></result>-->
<!-- <result property="userPwd" column="user_pwd"></result>-->
<!-- </resultMap>-->
<!-- 查询所有用户-->
<select id="queryAllUser" resultType="User">
select *
from mybatis.user
</select>
<!--通过id查询用户-->
<select id="queryUserById" parameterType="int" resultType="User">
select *
from mybatis.user
where user_id = #{userId}
</select>
<!-- 新增用户-->
<insert id="saveUser" parameterType="User">
insert into mybatis.user(user_name, user_pwd)
VALUES (#{userName}, #{userPwd})
</insert>
<!-- 更新用户-->
<update id="updateUser" parameterType="User">
update mybatis.user
set user_name =#{userName},
user_pwd=#{userPwd}
where user_id = #{userId};
</update>
</mapper>
5.service层
UserService
package com.song.service;
import com.song.pojo.User;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface UserService {
//查询所有用户
List<User> queryAllUser();
//通过Id查询用户
User queryUserById(int userId);
//新增用户
int saveUser(User user);
//更新用户
int updateUser(User user);
}
UserServiceImpl
package com.song.service;
import com.song.mapper.UserMapper;
import com.song.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> queryAllUser() {
return userMapper.queryAllUser();
}
@Override
public User queryUserById(int userId) {
return userMapper.queryUserById(userId);
}
@Override
public int saveUser(User user) {
return userMapper.saveUser(user);
}
@Override
public int updateUser(User user) {
return userMapper.updateUser(user);
}
}
6.controller
UserController
package com.song.controller;
import com.song.mapper.UserMapper;
import com.song.pojo.User;
import com.song.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/getUsers")
//查询所有用户
public List<User> getUsers() {
List<User> userList = userService.queryAllUser();
for (User user : userList) {
System.out.println(user);
}
return userList;
}
@GetMapping("/getUser/{userId}")
//通过id查询用户
public User getUser(@PathVariable int userId) {
User user = userService.queryUserById(userId);
return user;
}
@GetMapping("/saveUser")
//新增用户
public String saveUser() {
User user = new User(0, "小华", "3456");
int i = userService.saveUser(user);
String msg;
if (i > 0) {
msg = "插入成功";
} else {
msg = "插入失败";
}
return msg;
}
//更新用户
@GetMapping("/updateUser/{userId}/{userName}/{userPwd}")
public String updateUser(@PathVariable int userId, @PathVariable String userName, @PathVariable String userPwd) {
User user = new User(userId, userName, userPwd);
int flag;
flag = userService.updateUser(user);
String msg;
if (flag > 0) {
msg = "插入成功";
} else {
msg = "插入失败";
}
return msg;
}
}