文章目录
1.映射文件XxxMapper.xml
1.1语法
<?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="namespace="cn.kgc.mapper.UserMapper"">
<select id="findCount" …
……
</select>
</mapper>
1.2namespace:命名空间
namespace的命名必须跟某个接口同名。
1.3id:命名空间中唯一的标识符
接口中的方法名与映射文件中的SQL语句id一一对应
1.4parameterType:参数类型
传入SQL语句的参数类型(接口的形参)
1.5resultType:返回值
SQL语句返回值类型的完整类名或别名,也就是接口中的方法返回值。
1.6示例
需求:根据用户名对用户表进行模糊查询。
(1)创建数据库
(2)创建工程、目录
(3)pom.xml配置依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
(4)创建实体类
package cn.kgc.entity;
public class User {
private Integer id;
private String userCode;
private String userName;
private String userPassword;
private Integer gender;
private String birthday;
private String phone;
private String address;
private Integer userRole;
private Integer createdBy;
private String creationDate;
private Integer modifyBy;
private String modifyDate;
private String userRoleName;
public User() {
}
public User(String userCode, Integer userRole) {
this.userCode = userCode;
this.userRole = userRole;
}
public User(String userCode, String userName, String userPassword, Integer gender, String birthday, String phone, String address, Integer userRole, Integer createBy, String creationDate, Integer modifyBy, String modifyDate) {
this.userCode = userCode;
this.userName = userName;
this.userPassword = userPassword;
this.gender = gender;
this.birthday = birthday;
this.phone = phone;
this.address = address;
this.userRole = userRole;
this.createdBy = createBy;
this.creationDate = creationDate;
this.modifyBy = modifyBy;
this.modifyDate = modifyDate;
}
public User(Integer id, String userCode, String userName, String userPassword, Integer gender, String birthday, String phone, String address, Integer userRole, Integer createBy, String creationDate, Integer modifyBy, String modifyDate) {
this.id = id;
this.userCode = userCode;
this.userName = userName;
this.userPassword = userPassword;
this.gender = gender;
this.birthday = birthday;
this.phone = phone;
this.address = address;
this.userRole = userRole;
this.createdBy = createBy;
this.creationDate = creationDate;
this.modifyBy = modifyBy;
this.modifyDate = modifyDate;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String