什么是 MyBatis ?
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
如果使用 Maven 来构建项目,则需将下面的 dependency 代码置于 pom.xml 文件中:
配置Mybatis
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
定义实体Bean
package com.pay.entity;
public class Manager {
private Integer id;
private String username;
private String userpwd;
private String email;
public Manager(Integer id, String username, String userpwd, String email) {
this.id = id;
this.username = username;
this.userpwd = userpwd;
this.email = email;
}
public Manager() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd == null ? null : userpwd.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", username=").append(username);
sb.append(", userpwd=").append(userpwd);
sb.append(", email=").append(email);
sb.append("]");
return sb.toString();
}
}
package com.pay.entity;
public class Manager {
private Integer id;
private String username;
private String userpwd;
private String email;
public Manager(Integer id, String username, String userpwd, String email) {
this.id = id;
this.username = username;
this.userpwd = userpwd;
this.email = email;
}
public Manager() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd == null ? null : userpwd.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", username=").append(username);
sb.append(", userpwd=").append(userpwd);
sb.append(", email=").append(email);
sb.append("]");
return sb.toString();
}
}
定义Dao接口
package com.pay.dao;
import com.pay.entity.Manager;
import java.util.List;
public interface IManagerDao {
/**
* 添加一个管理员
* @param bean 管理实体类
*/
public void add(Manager bean);
/**
* 查询管理用户
* @param userName 用户名
* @param userPwd 用户密码
* @return 一个管理角色对象
*/
public Manager findUser(String userName,String userPwd);
/**
* 更改管理员密码
* @param userName 用户名
* @param newPwd 用户新密码
*/
public void updatePwd(String userName,String newPwd);
/**
* 查询所有管理员用户
* @return 管理员集合
*/
public List<Manager> findAll();
}
定义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.pay.dao.IManagerDao">
<!-- 缓存配置 -->
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
<resultMap id="ManagerResultMap" type="com.pay.entity.Manager">
<constructor>
<idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER" />
<arg column="userName" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="userPwd" javaType="java.lang.String" jdbcType="VARCHAR" />
<arg column="email" javaType="java.lang.String" jdbcType="VARCHAR" />
</constructor>
</resultMap>
<sql id="ManagerList">
id, userName, userPwd, email
</sql>
<!-- 添加管理员 -->
<insert id="add" parameterType="com.pay.entity.Manager">
insert into manager (userName, userPwd,
email)
values (#{username,jdbcType=VARCHAR}, #{userpwd,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR})
</insert>
<!-- 查询管理员 -->
<select id="findUser" resultMap="ManagerResultMap">
select <include refid="ManagerList"/> from manager where userName = #{userName} and userPwd = #{userPwd}
</select>
<!-- 更改管理员密码 -->
<update id="updatePwd">
UPDATE manager SET userPwd = #{userPwd} where userName = #{userName}
</update>
</mapper>
文章评论