一、通用mapper
pom.xml配置文件
<dependency>
<groupId>com.github.abel533</groupId>
<artifactId>mapper</artifactId>
<version>3.0.1</version>
</dependency>
在主配置文件中配置通用mapper
<plugins>
<plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
<property name="mappers" value="com.github.abel533.mapper.Mapper" />
</plugin>
<plugins>
SqlSessionFactory sf = SqlSessionFactoryUtil.sf();
SqlSession sqlSession = sf.openSession();
SongsMapper sm = sqlSession.getMapper(SongsMapper.class);
/**
* 查询Songs列表
* 通用mapper 查询只有一个select 方法,传值就是查询,传null是查全部
*/
public List<Songs> findAll() {
List<Songs> list = sm.select(null);
list.forEach(System.out::println);
}
/***
* 查询整个对象,确保数据唯一
*/
public User findById(Integer id) {
Songs s = new Songs();
s.setName("晴天");
Songs songs = sm.selectOne(s);
}
/***
* 查询数据总数
*/
public void add(User User) {
int i = sm.selectCount(null);
}
/*删除*/
public void delete() {
sm.deleteByPrimaryKey(888);
sqlSession.commit();
}
/*更新
*updateByPrimaryKey
*根据主键更新实体全部字段,null值会被更新
*updateByPrimaryKeySelective
*根据主键更新属性不为null的值
*/
public void update() {
Songs songs = new Songs(888,"张三",null,null,null,null);
sm.updateByPrimaryKeySelective(songs);
sqlSession.commit();
}
二、分页
pxm.xml配置
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.1</version>
</dependency>
主配置文件
<!-- 分页-->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,
SQLite,Hsqldb,PostgreSQL六种数据库 -->
<property name="helperDialect" value="mysql" />
<!-- 分页合理化-->
<property name="reasonable" value="true"/>
</plugin>
实例:
Page page = PageHelper.startPage(-1, 10);
List<Songs> list =sm.find("周杰伦");
list.forEach(System.out::println);
System.out.println("当前页"+page.getPageNum());
System.out.println("总条数"+page.getTotal());
System.out.println("总页数"+page.getPages());
System.out.println("每页条数"+page.getPageSize());
- 进行了分页合理化处理,在主配置文件中,设置reasonable 为true ,传入的页码为-1 默认从第一页开始,如果传入的页码不存在,则从查询最后一页
三、动态sql
一、where、if 查询 (包括模糊查询)
只用 避免了出现两个where 的错误语句
<select id="find" resultType="emp">
select * from emp
<where>
<if test="ename!=null and ename!=''">
ename like '%${ename}%'
</if>
<if test="job!=null and job!=''">
and ename like '%${job}%'
</if>
</where>
</select>
二、choose 、when 查询
- 如果满足when中条件的一种,则进行满足条件的查询,如果都不满足,则进行otherwise语句
<select id="chose" resultType="emp">
select * from emp
<choose>
<when test="ename!=null">
where ename like '%${ename}%'
</when>
<when test="job!=null">
where job like '%${job}%'
</when>
<otherwise>
order by sal desc
</otherwise>
</choose>
</select>
三、in 的查询
- collection属性中,如果查询的是集合使用list,如果是数据使用array
<select id="for1" resultType="emp">
select * from emp where deptno in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
<select id="for2" resultType="emp">
select * from emp where job in
<foreach collection="array" item="job" open="(" close=")" separator=",">
#{job}
</foreach>
</select>
- 使用
- Arrays.asList 数组转集合,只能读取,不能做增删改的处理
Integer[] id = {10, 20, 31}; // 测试集合
String[] jobs = {"job", "CLERK"}; // 测试数组
/* List<Emp> list = em.for1(Arrays.asList(id));
list.forEach(System.out::println);*/
List<Emp> list1 = em.for2(jobs);
list1.forEach(System.out::println);