SpringBoot整合Mybatis
新建springboot工程选中spring web、jdbc API、Mysql Driver
导入mybatis依赖、Lombok依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
数据库连接配置
# 应用名称
spring.application.name=springboot-02
# 应用服务 WEB 访问端口
server.port=8080
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=123456
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true$characterEncoding=utf-8
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
创建实体类:
package com.cjp.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
创建Mapper接口:
package com.cjp.mapper;
import com.cjp.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
//表示这是一个mybatis的mapper类
@Mapper
@Repository
public interface UserMapper {
List<User> queryUserList();
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
在application.properties中配置mybatis包别名以及mapper.xml的地址
#整合mybatis
mybatis.type-aliases-package=com.cjp.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
在Resource新建mybatis包—>mapper包,新建UserMapper.xml,编写UserMpper.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.cjp.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from user
</select>
<select id="queryUserById" resultType="User">
select * from user where id = #{id};
</select>
<insert id="addUser" parameterType="User">
insert into user (id,name,pwd) values (#{id},#{name},#{pwd});
</insert>
<update id="updateUser" parameterType="User">
update user set name = #{name},pwd=#{pwd} where id=#{id};
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
新建controller包–>编写UserController:
package com.cjp.controller;
import com.cjp.mapper.UserMapper;
import com.cjp.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
@GetMapping("/queryUserList")
public List<User> queryUserList(){
List<User> users = userMapper.queryUserList();
for (User user:users) {
System.out.println(user);
}
return users;
}
@GetMapping("/queryUserById")
public User queryUserById(){
User user = userMapper.queryUserById(1009);
return user;
}
@GetMapping("/addUser")
public String addUser(){
userMapper.addUser(new User(1009,"小锦儿","2132132"));
return "addUser_ok";
}
@GetMapping("/deleteUser")
public String deleteUser(){
userMapper.deleteUser(1009);
return "deleteUser_ok";
}
@GetMapping("/updateUser")
public String updateUser(){
userMapper.updateUser(new User(1009,"小驴儿","213213asdadsda2"));
return "deleteUser_ok";
}
}
CRUD完成 测试
项目结构: