Mybatis基础
安装mysql。在Ubuntu wsl中安装
初始化一个数据库用于测试
create database community;
use community;
source /home/***/Project/defaultTmpMvn/sql/init_schema.sql
source /home/***/Project/defaultTmpMvn/sql/init_data.sql
连接池(数据源):管理连接的工厂。能够统一的初始化一些连接,反复使用;管理连接的上限
# 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=root
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
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity
mybatis.configuration.useGeneratedKeys=true
mybatis.configuration.mapUnderscoreToCamelCase=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
数据库的驱动,从Mysql的官方手册查到
spring.datasource.url=jdbc:mysql://localhost:3306/community?characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong
mysql连接的路径,固定的格式
spring.datasource.username=root
spring.datasource.password=***
账号密码
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
连接池的类。连接池有很多,Spring集成了很多,这是性能最好的
spring.datasource.hikari.maximum-pool-size=15
连接池的最大连接数
spring.datasource.hikari.minimum-idle=5
最小空闲连接(大批请求出现,请求结束,连接空闲,需要回收几个?)
spring.datasource.hikari.idle-timeout=30000
超时时间(连接空闲多久以后,才回收?)
mybatis.mapper-locations=classpath:mapper/*.xml
映射文件的存放位置。在resources目录下建一个mapper,编译后就会到target/classes文件夹下,所以写classpath
mybatis.type-aliases-package=com.nowcoder.community.entity
实体类的包名。用实体类封装表里的数据,引用实体类的时候,有这一项就不用写包名了。
mybatis.configuration.useGeneratedKeys=true
自动生成组件
mybatis.configuration.mapUnderscoreToCamelCase=true
表中的字段是不区分大小写的。这个是让驼峰式命名和下划线命名二者自动匹配起来。(header_url和headerUrl)
让表里的字段和类中的属性匹配,就可以达到自动的状态,不用逐个设置了
在entity中建立一个实体类,对照数据库的属性名写字段
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;
}
然后IDE自动生成Getter和Setter即可(ALT + insert for IDEA),可以再加一个toString
为了访问数据库,在dao层写一个组件,mybatis管组件叫mapper,只需要写接口,不用写实现类。
需要加一个注解让Spring容器装配Bean
用mabatis的@Mapper注解
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
User selectById(int id);
User selectByName(String username);
User selectByemail(String email);
int insertUser(User user);
int updateStatus(int id,int stauts);
int updateHeader(int id,String headerUrl);
int updatePassword(int id,String password);
}
在mapper目录下创建一个xml文件,文件结构从mybatis官网上https://mybatis.org/mybatis-3/zh/getting-started.html即可找到
然后看如何写那个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.example.demo.dao.UserMapper">
<sql id="selectFields">
id,username,password,salt,email,type,status,activation_code,header_url,create_time
</sql>
<sql id="insertFields">
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},#{activation_code},#{header_url},#{create_time})
</insert>
<update id="updateStatus">
update user set status = #{status} where id = #{id}
</update>
<update id="updateHead">
update user set header_url = #{header_url} where id = #{id}
</update>
<update id="updatePassword">
update user set password = #{password} where id = #{id}
</update>
</mapper>
mapper的namespace写dao中的userMapper所在。
parameterType resultType在不是基本类型的时候要指定。包名在application.properties里说明了,它才能找到,否则要在这里指定