1-创建项目的目录结构
2-在pojo中创建实体类User
package cn.smbms.pojo;
import java.util.Date;
//用户实体类 方法:setter和getter方法
public class User {
private Integer id;//主键id
private String userCode;//用户编码
private String userName;//用户名称
private String userPassword;//用户密码
private String gender;//用户性别
private Date birthday;//出生日期
private String phone;//电话
private String address;//地址
private Integer userRole;//用户角色
private Integer createdBy;//创建人
private Date creationDate;//创建时间
private Integer modifyBy;//修改人
private Date modifyDate;//修改时间
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 getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
3-在dao.user包中创建UserMapper.java和UserMapper.xml
dao包:负责和数据进行交互的代码和操作
UserMapper.java
package cn.smbms.dao.user;
public interface UserMapper {
//需求是:统计用户表中的记录数
// select count(1) as count from smbms_user ;
public int count();
}
UserMapper.xml
https://mybatis.org/mybatis-3/getting-started.html
sql映射文件
<?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="cn.smbms.dao.user.UserMapper">
<!--id 填写接口中的方法名 resultType 填写 返回类型-->
<select id="count" resultType="int">
select count(1) as count from smbms_user
</select>
</mapper>
4-在mybatis-config.xml文件中添加sql映射文件的引入
<!--引入sql映射文件-->
<mappers>
<mapper resource="cn/smbms/dao/user/UserMapper.xml"/>
</mappers>
5-测试类
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
public class UserMapperTest {
//创建日志对象
Logger logger=Logger.getLogger(UserMapperTest.class);
//测试方法
@Test
public void test(){
SqlSession sqlSession=null;
try {
//1 获得mybatis-config.xml文件的输入流
InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
//2 创建SqlSessionFactory对象,读取上述流
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//3 创建SqlSession对象(Connection)
sqlSession=sqlSessionFactory.openSession();
//4 通过sqlSession对象对mapper文件对应的接口进行方法的读取 全路径+方法名
int count = sqlSession.selectOne("cn.smbms.dao.user.UserMapper.count");
logger.debug("UserMapperTest test count:"+count);
} catch (IOException e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
}