1.MyBatis映射文件深入
if 和 foreach&片段抽取
<?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.lemon.mapper.UserMapper">
<!-- sql片段抽取-->
<sql id="apple">
select * from user
</sql>
<select id="findByIds" resultType="user" parameterType="list">
-- select * from user where id in(1,3)
-- sql片段引用
<include refid="apple"/>
<where>
<foreach collection="list" open="id in(" close=")" item="id" separator=",">
${id}
</foreach>
</where>
</select>
<select id="findAll" resultType="user">
select * from user
</select>
<select id="findUser" resultType="user" parameterType="int">
select * from user
<where>
<if test="id!=0">
and id=${id}
</if>
</where>
</select>
</mapper>
复制代码
public class ServiceDemo {
public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// List<User> userList = mapper.findAll();
User user=new User();
user.setId(1);
// List<User> userList = mapper.findUser(user);
List<Integer> ids=new ArrayList<>();
ids.add(1);
ids.add(3);
List<User> userList = mapper.findByIds(ids);
System.out.println(userList);
}
}
复制代码
MyBatis核心配置文件深入
typeHandlers标签
自定义typeHandler
例子:
在user中增加birthday 字段定义为bigint类型
在User类中添加birthday属性为Date类型
,发现他们无法自动转换,
于是自定义TypeHandler
1.创建一个类继承BaseTypeHandler
package com.lemon.handler;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
public class DateHandler extends BaseTypeHandler<Date> {
//把java数据类型转换为sql中的数据类型
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
long time = date.getTime();
preparedStatement.setLong(i,time); //把java类型转换为long型存储在数据库中
}
//把数据库中的类型转换为java的数据类型
@Override
public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
long time = resultSet.getLong(s);
Date date=new Date(time);
return date;
}
@Override
public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
long time = resultSet.getLong(i);
Date date = new Date(time);
return date;
}
@Override
public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
long time = callableStatement.getLong(i);
Date date = new Date(time);
return date;
}
}
复制代码
2.在sqlMapConfig.xml中注册类处理器
<typeHandlers>
<typeHandler handler="com.lemon.handler.DateHandler"/>
</typeHandlers>
复制代码
1.2 plugins标签
1.导入pom依赖
<!-- 引入分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<!-- 解析器-->
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
复制代码
2.在sqlMapConfig.xml中注册
<!-- 配置分页插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
复制代码
3.测试
@Test
public void test2() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
PageHelper.startPage(1,2);
List<User> userList = mapper.findAll();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
链接:https://juejin.cn/post/7032673348555374599