Mybatis课程链接
用于创建SqlSession的工厂类
向数据库执行SQL
连接数据参数,配置连接池
通过maven.repository官网加载jar包,将官网中的代码复制粘贴(注意要包含版本,否则会报错)
配置MySQL和Mybatis,模板在MySQL官网手册
#DataSourceProperties
配置数据库和连接池数据源
连接池定义:统一管理连接的工厂
作用:
就是Dao接口,不需要写实现类,自动实现接口,前提是要已知增删改查的方法和依赖的SQL语句
/entity
package com.nowcoder.community.entity;
import java.util.Date;
//实体类:封装表里的数据,对表进行增删查改
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;
//GET和SET方法
//Alt+Insert快捷键
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getActivationCode() {
return activationCode;
}
public void setActivationCode(String activationCode) {
this.activationCode = activationCode;
}
public String getHeaderUrl() {
return headerUrl;
}
public void setHeaderUrl(String headerUrl) {
this.headerUrl = headerUrl;
}
public Date getCreatTime() {
return createTime;
}
public void setCreatTime(Date creatTime) {
this.createTime = creatTime;
}
//ToString Alt+Insert快捷键
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", salt='" + salt + '\'' +
", email='" + email + '\'' +
", type=" + type +
", status=" + status +
", activationCode='" + activationCode + '\'' +
", headerUrl='" + headerUrl + '\'' +
", creatTime=" + createTime +
'}';
}
}
UserMapper.java
package com.nowcoder.community.dao;
import com.nowcoder.community.entity.User;
import org.apache.ibatis.annotations.Mapper;
//@Repository
//Mybatis注解
@Mapper
public interface UserMapper {
//根据id查询user
User selectById(int id);
//根据name查
User selectByName(String username);
//根据email查
User selectByEmail(String email);
//增加一个用户,返回整数,插入数据的行数,参数是整个user包含的数据
//用user对象封装
int insertUser(User user);
//修改用户状态,返回修改条数,修改几行数据,以id为条件传入新的状态码
int updateStatus(int id, int status);
//更新头像路径,以id为条件传入新的headerUrl
int updateHeader(int id, String headerUrl);
//更新密码
int updatePassword(int id, String password);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Mybatis官网 -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace写UserMapper权限定名-->
<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>
<!-- 写标签,每一个标签对应mapper 引用方法的参数名为id -->
<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>
<!-- id和方法名对应,复杂的参数类型需要声明parameterType,-->
<!-- keyProperty表明与id字段对应的属性名 增加的时候MySQL自动生成id,被Mybatis得到填入本来增加的时候id不存在,-->
<!-- id是Mybatis从数据库获取之后回填进user-->
<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>
数据库表备注:
user表 |
---|
salt(盐): 密码后面加的随机字符串,防止密码过于简答(随机生成) |
type 用户类型 0普通用户 1管理员 2版主 |
status 用户状态 0未激活 1激活 |
activation_code 激活码 |
create_time 注册时间 |