文章目录
一 MyBatis简介 二 配置数据源 三 整合测试 3.1 导入MyBatis依赖 3.2 配置数据库连接信息application.yml 3.3 使用idea测试数据库 3.4 创建实体类【可以使用Lombok】 3.5 创建实体类 3.6 创建mapper目录以及对应的Mapper接口 3.7 创建Mapper映射文件 3.8 配置mybatis映射文件 3.9 解决问题:配置别名xml文件中依旧爆红 3.10 测试
一 MyBatis简介
二 配置数据源
创建项目步骤及数据库内容同Spring Boot操作数据库学习之整合JDBC2.1~2.3
三 整合测试
3.1 导入MyBatis依赖
< dependency>
< groupId> org.mybatis.spring.boot</ groupId>
< artifactId> mybatis-spring-boot-starter</ artifactId>
< version> 2.2.2</ version>
</ dependency>
3.2 配置数据库连接信息application.yml
spring :
datasource :
username : root
password : xxxx
url : jdbc: mysql: //localhost: 3306/mybatis? useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
3.3 使用idea测试数据库
1. 打开右侧侧边栏中的数据库
2. 点击新打开窗口的左侧+号图标
3. 选择数据源类型
4. 填写用户名和密码
5. 点击测试按钮进行连接测试
6. 选择架构,选择使用的数据库,点击确定即可
3.4 创建实体类【可以使用Lombok】
3.5 创建实体类
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
3.6 创建mapper目录以及对应的Mapper接口
import com. yang. pojo. User ;
import org. apache. ibatis. annotations. Mapper ;
import org. springframework. stereotype. Repository ;
import java. util. List ;
@Mapper
@Repository
public interface UserMapper {
List < User > queryUserList ( ) ;
User queryUserById ( int id) ;
int addUser ( User user) ;
int updateUser ( User user) ;
int deleteUser ( int id) ;
}
3.7 创建Mapper映射文件
<?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.yang.mapper.UserMapper" >
< select id = " queryUserList" resultType = " User" >
select * from user;
</ select>
< select id = " queryUserById" resultType = " User" parameterType = " int" >
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>
3.8 配置mybatis映射文件
在spring boot[applicaton.yml]中添加别名配置信息
mybatis :
type-aliases-package : com.yang.pojo
mapper-locations : classpath: mybatis/mapper/*.xml
3.9 解决问题:配置别名xml文件中依旧爆红
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
import org. apache. ibatis. type. Alias ;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Alias ( "User" )
public class User {
private int id;
private String name;
private String pwd;
}
3.10 测试
import com. yang. mapper. UserMapper ;
import com. yang. pojo. User ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
import java. util. List ;
@RestController
public class UserController {
@Autowired
UserMapper userMapper;
@RequestMapping ( "/queryUserList" )
public List < User > queryUserList ( ) {
List < User > userList = userMapper. queryUserList ( ) ;
for ( User user: userList ) {
System . out. println ( user) ;
}
return userList;
}
@RequestMapping ( "/queryUserById" )
public User queryUserById ( ) {
return userMapper. queryUserById ( 5 ) ;
}
@RequestMapping ( "/addUser" )
public String addUser ( ) {
userMapper. addUser ( new User ( 6 , "三毛" , "123456" ) ) ;
return "finish addUser" ;
}
@RequestMapping ( "/updateUser" )
public String updateUser ( ) {
userMapper. updateUser ( new User ( 6 , "阿毛" , "5211314" ) ) ;
return "finish UpdateUser" ;
}
@RequestMapping ( "/deleteUser" )
public String deleteUer ( ) {
userMapper. deleteUser ( 6 ) ;
return "finish DeleteUser" ;
}
}