Mybatis深入
详谈generator生成文件
实体类
-
生成的实体类完全对应数据库中的表,表中的一列对应实体类中的一个属性
-
实体类封装的步骤
- 私有化成员变量
- 提供公共的getter、setter方法
- 需要用到的构造方法
-
使用属性封装
DAO(Mapper)
- 根据实体类中的属性自动生成基本的增删改查接口
- 以UserMapper为例
public interface EasybuyUserMapper {
int deleteByPrimaryKey(Integer id);
int insert(EasybuyUser record);
int insertSelective(EasybuyUser record);
EasybuyUser selectByPrimaryKey(Integer id);
List<EasybuyUser> selectUserByType(Integer type);
int updateByPrimaryKeySelective(EasybuyUser record);
int updateByPrimaryKey(EasybuyUser record);
}
Mapping文件
详细解释参考下面的注释内容
<?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="此配置文件将要实现的接口(对应于DAO中的接口)">-->
<mapper namespace="com.xhy.dao.EasybuyUserMapper" >
结果映射集 id必须唯一否则将解析失败代表这个映射的名称 type为对应实体类
<resultMap id="BaseResultMap" type="com.xhy.pojo.EasybuyUser" >
<!--主键 colum为数据库(sql语句)中属性名称 property为实体类中的名字必须一致-->
<id column="id" property="id" jdbcType="INTEGER" />
同上类似
<result column="loginName" property="loginname" jdbcType="VARCHAR" />
<result column="userName" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="sex" property="sex" jdbcType="INTEGER" />
<result column="identityCode" property="identitycode" jdbcType="VARCHAR" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="mobile" property="mobile" jdbcType="VARCHAR" />
<result column="type" property="type" jdbcType="INTEGER" />
</resultMap>
一段sql语句可以被插入到具体sql语句中 常用于把重复率高的部分整合出来
<sql id="Base_Column_List" >
id, loginName, userName, password, sex, identityCode, email, mobile, type
</sql>
selcet语句 id="DAO接口中的一个方法" 结果集映射 传入参数类型
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />插入前面的sql部分语句
from easybuy_user
where id = #{id,jdbcType=INTEGER} #{}为参数占位符对应传进来的参数
</select>
<select id="selectUserByType" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select
id,loginname,username
from easybuy_user
where type=#{type}
</select>
删除语句
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from easybuy_user
where id = #{id,jdbcType=INTEGER}
</delete>
插入语句 配置对应主键 开启主键自动递增
<insert id="insert" parameterType="com.xhy.pojo.EasybuyUser" keyProperty="id" useGeneratedKeys="true" >
insert into easybuy_user ( loginName, userName,
password, sex, identityCode,
email, mobile, type
)
values (#{loginname,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR}, #{sex,jdbcType=INTEGER}, #{identitycode,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER}
)
</insert>
更新语句标签
<update id="updateByPrimaryKey" parameterType="com.xhy.pojo.EasybuyUser" >
update easybuy_user
set loginName = #{loginname,jdbcType=VARCHAR},
userName = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
sex = #{sex,jdbcType=INTEGER},
identityCode = #{identitycode,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
mobile = #{mobile,jdbcType=VARCHAR},
type = #{type,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
完成此步写测试方法之前一定要在sqlmapconfig.xml文件中为此文件进行相关配置
核心接口和类
- sqlSessionFactor
- sqlSession
//将配置文件中的信息内容读取到流中
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//利用配置信息中的数据库相关信息实例化sqlSession创建工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//通过创建工厂来实例化一个可用的数据库会话层,可以用来执行已经映射的sql语句
SqlSession sqlSession = sqlSessionFactory.openSession();
自定义Mapper方法
- 以用户登录为例
1. 在Dao包中的找到EasybuyUserMapper接口,添加抽象方法
EasybuyUser userLogin(EasybuyUser user);
方法名自定义,返回值类型和参数类型根据实际需要
2.在EasybuyUserMapper.xml文件中完成相关配置
- 先利用navicat测试要添加的sql语句,确保能得到自己想要的结果
- id为要映射实现的dao中接口的抽象方法,也就是上面刚刚添加的,确保名字一致
- 传入参数用#{}来占位,内容需要与实体类中保持一致否则无法进行匹配
<select id="userLogin" parameterType="com.xhy.pojo.EasybuyUser" resultMap="BaseResultMap">
select id, loginName, userName, sex, identityCode, email, mobile, type
from easybuy_user
where loginname=#{loginname,jdbcType=VARCHAR} AND password=#{password,jdbcType=VARCHAR}
</select>
3.若改配置文件未在SqlMapConfig.xml中注册过则先进行注册
配置内容如下
<mappers>
<mapper resource="mapping/EasybuyUserMapper.xml"></mapper>
</mappers>
4.写测试方法
以下根据最新的官方文档推荐的方法来写与课上老师略有不同
@Test
public void loginTest(){
//使用工具类生成sql临时会话调用相关sql语句
SqlSession sqlSession = MybatisUtils.getSqlSession();
//利用sqlSession根据配置文件生成的类实例化mapper
EasybuyUserMapper mapper = sqlSession.getMapper(EasybuyUserMapper.class);
//通过mapper来调用相关方法会方便很多,也很好理解
EasybuyUser user = new EasybuyUser("admin","e10adc3949ba59abbe56e057f20f883e");
EasybuyUser user1 = mapper.userLogin(user);
System.out.println(user1);
sqlSession.close();
}