前往顾页
以后地位: 主页 > 收集编程 > Jsp实例教程 >

MyBatis 利用详解

时候:2018-08-28 00:45来源:知行网www.zhixing123.cn 编辑:麦田守望者

甚么是 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>

 


------分开线----------------------------
标签(Tag):MyBatis
------分开线----------------------------
保举内容
猜你感兴趣