1、Mybatis代理开发
只需要编写Mapper 接口,Mybatis 框架根据接口定义创建接口的动态代理对象
Mapper 接口开发需要遵循以下规范:
-
Mapper.xml文件中的namespace与mapper接口的全限定名相同
-
Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
-
Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql的parameterType的类型相同
-
Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
UserMapper.java
public interface UserMapper { User findUserById(int id); }
UserMapper.xml
<mapper namespace="cn.guixinchn.mapper.UserMapper"> <select id="findUserById" resultType="cn.guixinchn.domain.User" parameterType="int"> select * from user where id = #{id} </select> </mapper>
Test
@Test //MyBatis代理开发 public void test5() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); //获得MyBatis框架生成的UserMapper接口的实现类 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.findUserById(2); System.out.println(user); sqlSession.close(); }
结果:
User{id=2, username=‘lisi‘, password=‘123‘}
2、MyBatis动态sql
在Mapper.xml文件中可以使用:<where>
、<if>
、<foreach>
等标签
例子一:
<select id="findUserByCondition" resultType="user" parameterType="user">
select * from user
<where>
<if test="id!=0">
and id = #{id}
</if>
<if test="username!=null">
and username = #{username}
</if>
</where>
</select>
@Test
//MyBatis动态sql
public void test6() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//测试数据
User condition = new User();
condition.setUsername("lisi");
//获得MyBatis框架生成的UserMapper接口的实现类
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User userByCondition = userMapper.findUserByCondition(condition );
System.out.println(userByCondition);
sqlSession.close();
}
例子二:
<select id="findUserByIds" resultType="user" parameterType="list">
select * from user
<where>
<foreach collection="list" item="id" open="id in (" close=")" separator=",">
#{id}
</foreach>
</where>
</select>
@Test
//MyBatis动态sql
public void test7() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//测试数据
List arrayList = new ArrayList();
arrayList.add(1);
arrayList.add(2);
//获得MyBatis框架生成的UserMapper接口的实现类
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.findUserByIds(arrayList);
System.out.println(userList);
sqlSession.close();
}
foreach标签的属性:
- collection:代表要遍历的集合元素,注意编写时不要写#{}
- open:代表语句的开始部分
- close:代表结束部分
- item:代表遍历集合的每个元素,生成的变量名
- sperator:代表分隔符
3、SQL片段抽取
<!--将重复的sql提取出来-->
<sql id="selectAll">select * from user</sql>
<select id="findUserByIds" resultType="user" parameterType="list">
<!--用include引用-->
<include refid="selectAll"></include>
<where>
<foreach collection="list" item="id" open="id in (" close=")" separator=",">
#{id}
</foreach>
</where>
</select>
4、MyBatis核心配置文件
4.1typeHandlers标签
重写类型处理器或创建你自己的类型处理器来处理不支持的或非标准的类型
开发步骤:
-
定义转换类继承类
BaseTypeHandler<T>
(指定泛型) -
覆盖4个未实现的方法,其中setNonNullParameter为java程序设置数据到数据库的回调方法,getNullableResult为查询时 mysql的字符串类型转换成 java的Type类型的方法
比如下面是java的date类型,转换成数据库的bigint类型:
public class MyDateTypeHandler extends BaseTypeHandler<Date> { //把java类型数据转换成数据库需要的类型 public void setNonNullParameter(PreparedStatement ps, int i, Date date, JdbcType jdbcType) throws SQLException { long time = date.getTime(); ps.setLong(i,time); } //数据库数据类型转换成java实体类对应的类型 //ResultSet:查询出的结果集、String:要转换的字段名称 public Date getNullableResult(ResultSet rs, String columnName) throws SQLException { long aLong = rs.getLong(columnName); Date date = new Date(aLong); return date; } public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException { long aLong = rs.getLong(columnIndex); Date date = new Date(aLong); return date; } public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { long aLong = cs.getLong(columnIndex); Date date = new Date(aLong); return date; } }
-
在MyBatis核心配置文件中进行注册
<!--注册类型转换器--> <typeHandlers> <typeHandler handler="cn.guixinchn.handler.MyDateTypeHandler"/> </typeHandlers>
4.2plugins标签
MyBatis可以使用第三方的插件来对功能进行扩展,比如:分页助手PageHelper
开发步骤:
-
导入通用PageHelper的坐标
<!--分页助手--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>1.0</version> </dependency>
-
在mybatis核心配置文件中配置PageHelper插件
<!-- 分页助手插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"/> </plugins>
-
测试分页数据获取
@Test //分页助手测试 public void test10() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); PageHelper.startPage(2,2); List<User> userList = userMapper.findAll(); for (User user : userList) { System.out.println(user); } //把userList的结果给PageInfo,推算出分页信息 PageInfo<User> pageInfo = new PageInfo<User>(userList); System.out.println("总条数:"+pageInfo.getTotal()); System.out.println("总页数:"+pageInfo.getPages()); System.out.println("当前页:"+pageInfo.getPageNum()); System.out.println("每页显示长度:"+pageInfo.getPageSize()); System.out.println("是否第一页:"+pageInfo.isIsFirstPage()); System.out.println("是否最后一页:"+pageInfo.isIsLastPage()); sqlSession.close(); }