参考牛客网高级项目教程
1.Mybatis核心组件
- SqlSessionFactory用于创建SqlSession的工厂,
- SqlSession:相当于JDBC的connection,用于向数据库执行SQL。
- xml主配置文件:XML配置文件,可以对MyBatis的底层行为做出详细的配置。
前两组核心组件,SpringBoot已经封装好了,直接使用注解拿到mapper接口的代理对象,即可操作sql
xml主配置文件,可以在SpringBoot的主配置application.properties中配置相关信息
- Mapper接口:就是DAO接口,在MyBatis中习惯性的称之为Mapper。
- Mapper映射器:用于编写SQL,并将SQL和实体类映射的组件,采用XML、注解均可实现。
- 示例
- 使用MyBatis对用户表进行CRUD操作。
2. Mybaits对user表的操作示例
1)环境搭建,依赖包的导入
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
2)实体类和接口的创建
public class User {
private int id;
private String username;
private String password;
private String salt;
private String email;
private int type;
private int status;
private String activationCode;
private String headerUrl;
private Date createTime;
}
@Mapper
public interface UserMapper {
User selectById(int id);
User selectByName(String name);
User selectByEmail(String email);
int insertUser(User user);
int updateStatus(int id, int status);
int updateHeader(int id, String headerUrl);
int updatePassword(int id, String password);
}
3)主配置文件的配置
# DataSourceProperties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/community?characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong
spring.datasource.username=#
spring.datasource.password=#
# 连接池相关配置
# 连接池类型
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
# 连接池中允许的最大连接数
spring.datasource.hikari.maximum-pool-size=15
# 池中维护的最小空闲连接数
spring.datasource.hikari.minimum-idle=5
# 连接池等待时间
spring.datasource.hikari.idle-timeout=30000
# MybatisProperties
# mapper映射配置:映射文件的存放位置
mybatis.mapper-locations=classpath:mapper/*.xml
# 实体类的别名,默认是改包下的所有类名,不区分大小写
mybatis.type-aliases-package=com.nowcoder.community.entity
# 启动自动生成主键
mybatis.configuration.useGeneratedKeys=true
# setting相关配置,实体类的变量命名与数据库表中变量的命名相匹配,即下划线与驼峰标识相匹配
mybatis.configuration.mapUnderscoreToCamelCase=true
4)映射文件sql的编写
- 注意:增加时候,要配置主键声明:keyProperty,mysql底层会自动生成id,mybatis会得到这
<?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.nowcoder.community.dao.UserMapper">
<sql id="insertFields">
username, password, salt, email, type, status, activation_code, header_url, create_time
</sql>
<sql id="selectFields">
id, username, password, salt, email, type, status, activation_code, header_url, create_time
</sql>
<select id="selectById" resultType="User">
select <include refid="selectFields"></include>
from user
where id = #{id}
</select>
<select id="selectByName" resultType="User">
select <include refid="selectFields"></include>
from user
where username = #{username}
</select>
<select id="selectByEmail" resultType="User">
select <include refid="selectFields"></include>
from user
where email = #{email}
</select>
<insert id="insertUser" parameterType="User" keyProperty="id">
insert into user(<include refid="insertFields"></include>)
values (#{username}, #{password}, #{salt}, #{email}, #{type},
#{status}, #{activationCode}, #{headerUrl}, #{createTime})
</insert>
<update id="updateStatus">
update user set status = #{status} where id = #{id}
</update>
<update id="updateHeader">
update user set header_url = #{headerUrl} where id = #{id}
</update>
<update id="updatePassword">
update user set password = #{password} where id = #{id}
</update>
</mapper>
5)SpringBoot整合junit测试
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class MapperTest {
@Autowired
UserMapper userMapper;
@Test
public void testSelectUser() {
User user = userMapper.selectById(101);
System.out.println(user);
user = userMapper.selectByEmail("nowcoder101@sina.com");
System.out.println(user);
user = userMapper.selectByName("liubei");
System.out.println(user);
}
@Test
public void testInsertUser() {
User user = new User();
user.setUsername("test");
user.setPassword("123456");
user.setSalt("abc");
user.setEmail("test@qq.com");
user.setHeaderUrl("http://www.nowcoder.com/101.png");
user.setCreateTime(new Date());
int rows = userMapper.insertUser(user);
System.out.println(rows);
System.out.println(user.getId());
}
@Test
public void testUpdateUser() {
int rows = userMapper.updateStatus(150, 1);
System.out.println(rows);
rows = userMapper.updateHeader(150, "http://www.nowcoder.com/102.png");
System.out.println(rows);
rows = userMapper.updatePassword(150, "hello");
System.out.println(rows);
}
}
|