Poetries.java
package com.jikexueyuan.entity;
import java.io.Serializable;
/**
* 诗句模型
*
* @author 黄建清
*
*/
public class Poetries implements Serializable {
private static final long serialVersionUID = -4657536584850251820L;
private Integer id;
private Poets poets;
private String content;
private String title;
public Poetries() {
}
public Poetries(Integer id, Poets poets, String content, String title) {
super();
this.id = id;
this.poets = poets;
this.content = content;
this.title = title;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContent() {
return this.content;
}
public void setContent(String content) {
this.content = content;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public Poets getPoets() {
return poets;
}
public void setPoets(Poets poets) {
this.poets = poets;
}
}
Poets.java
package com.jikexueyuan.entity;
import java.io.Serializable;
/**
* 诗人模型bean
*/
import java.util.List;
public class Poets implements Serializable {
private static final long serialVersionUID = 2255213471279022629L;
private Integer id;
private String name;
private List<Poetries> poetries;
public Poets() {
}
public Poets(String name) {
super();
this.name = name;
}
public Poets(Integer id, String name, List<Poetries> poetries) {
super();
this.id = id;
this.name = name;
this.poetries = poetries;
}
public Poets(Integer id) {
super();
this.id = id;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public List<Poetries> getPoetries() {
return poetries;
}
public void setPoetries(List<Poetries> poetries) {
this.poetries = poetries;
}
}
用来分装分页信息的model,pageModel.java
package com.jikexueyuan.entity;
import java.util.List;
/**
* 页面模型bean
* @author Administrator
*
* @param <T>
*/
public class PageModel<T> {
private Integer pageNumber; // 当前页数
private Integer pageSize; // 一页显示数量
@SuppressWarnings("unused")
private Integer startRow; // 查询起始行
private Integer total; // 总记录行数
@SuppressWarnings("rawtypes")
private List rows; // 查询结果数据
private T queryObj; // 查询对象
public PageModel() {
super();
}
public PageModel(T queryObj) {
super();
this.queryObj = queryObj;
}
public PageModel(Integer pageNumber, Integer pageSize, T queryObj) {
super();
this.pageNumber = pageNumber;
this.pageSize = pageSize;
this.queryObj = queryObj;
}
public PageModel(Integer pageNumber, Integer pageSize, Integer startRow,
Integer total, @SuppressWarnings("rawtypes") List rows, T queryObj) {
super();
this.pageNumber = pageNumber;
this.pageSize = pageSize;
this.startRow = startRow;
this.total = total;
this.rows = rows;
this.queryObj = queryObj;
}
public PageModel(Integer pageSize, Integer startRow) {
super();
this.pageSize = pageSize;
this.startRow = startRow;
}
@SuppressWarnings("rawtypes")
public List getRows() {
return rows;
}
@SuppressWarnings("rawtypes")
public void setRows(List rows) {
this.rows = rows;
}
public Integer getStartRow() {
if (pageNumber != null && pageSize != null) {
return (pageNumber - 1) * pageSize;
} else {
return 0;
}
}
public Integer getPageNumber() {
return pageNumber;
}
public void setPageNumber(Integer pageNumber) {
this.pageNumber = pageNumber;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public void setQueryObj(T queryObj) {
this.queryObj = queryObj;
}
public T getQueryObj() {
return queryObj;
}
}
mapper文件
PoetriesMapper.xml
<?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.jikexueyuan.dao.PoetriesDao">
<sql id="sql_count">
SELECT COUNT(*)
</sql>
<sql id="sql_select">
SELECT poetries.*,poets.*
</sql>
<sql id="sql_where">
FROM poets INNER JOIN poetries ON poets.id=poetries.poet_id
<where>
<if test="queryObj != null">
<!-- 如果选择输入名句,则按照名句搜索诗词 -->
<if test="queryObj.content != null and queryObj.content != ''">
AND content like CONCAT ('%',#{queryObj.content},'%')
</if>
<!-- 如果选择输入题目,则按照题目搜索诗词 -->
<if test="queryObj.title != null">
AND title = #{queryObj.title}
</if>
</if>
</where>
</sql>
<select id="selectPoeListWithPage" parameterType="com.jikexueyuan.entity.PageModel"
resultMap="poetyMap">
<include refid="sql_select"></include>
<include refid="sql_where"></include>
<!-- 分页 -->
<if test="pageNumber != null and pageSize!= null">
limit #{startRow},#{pageSize}
</if>
</select>
<select id="selectPoeCountWithPage" parameterType="com.jikexueyuan.entity.PageModel"
resultType="java.lang.Integer">
<include refid="sql_count"></include>
<include refid="sql_where"></include>
</select>
<resultMap type="Map" id="poetyMap">
<id column="id" property="poetries.id" />
<result column="title" property="title" />
<result column="content" property="content" />
<association property="poets" column="poet_id"
javaType="com.jikexueyuan.entity.Poets">
<id property="id" column="poets.id" />
<result property="name" column="name" />
</association>
</resultMap>
</mapper>
PoetsMapper.xml
<?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.jikexueyuan.dao.PoetsDao">
<!-- 嵌套查询 ,通过诗人名搜索标题与内容 -->
<sql id="sql_count">
SELECT COUNT(*)
</sql>
<sql id="sql_select">
SELECT poetries.*,poets.*
</sql>
<sql id="sql_where">
FROM poets INNER JOIN poetries ON poets.id=poetries.poet_id
<where>
<if test="queryObj != null">
<!-- 如果输入诗人,则按照诗人搜索诗词 -->
<if test="queryObj.name != null">
AND name = #{queryObj.name}
</if>
</if>
</where>
</sql>
<select id="findByName" parameterType="com.jikexueyuan.entity.PageModel"
resultMap="poetMap">
<include refid="sql_select"></include>
<include refid="sql_where"></include>
<!-- 分页 -->
<if test="pageNumber != null and pageSize!= null">
limit #{startRow},#{pageSize}
</if>
</select>
<resultMap type="Map" id="poetMap">
<id column="id" property="poetries.id" />
<result column="title" property="title" />
<result column="content" property="content" />
</resultMap>
<select id="countWithPage" parameterType="com.jikexueyuan.entity.PageModel"
resultType="java.lang.Integer">
<include refid="sql_count"></include>
<include refid="sql_where"></include>
</select>
</mapper>
dao层
PoetriesDao.java
package com.jikexueyuan.dao;
import java.util.List;
import java.util.Map;
import com.jikexueyuan.annotation.MyBatisRepository;
import com.jikexueyuan.entity.PageModel;
import com.jikexueyuan.entity.Poetries;
/**
* 诗词表的DAO组件
*/
@MyBatisRepository
public interface PoetriesDao {
@SuppressWarnings("rawtypes")
public List<Map> selectPoeListWithPage(PageModel<Poetries> pageModel);
public Integer selectPoeCountWithPage(PageModel<Poetries> pageModel);
}
Poets.java
package com.jikexueyuan.dao;
import java.util.List;
import java.util.Map;
import com.jikexueyuan.annotation.MyBatisRepository;
import com.jikexueyuan.entity.PageModel;
import com.jikexueyuan.entity.Poets;
/**
* 诗词表的DAO组件
*/
@MyBatisRepository
public interface PoetsDao {
@SuppressWarnings("rawtypes")
public List<Map> findByName(PageModel<Poets> pageModel);
public Integer countWithPage(PageModel<Poets> pageModel);
}
service层
PoetriesServiceImpl.java
package com.jikexueyuan.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.jikexueyuan.dao.PoetriesDao;
import com.jikexueyuan.entity.PageModel;
import com.jikexueyuan.entity.Poetries;
import com.jikexueyuan.service.PoetriesService;
@Service
public class PoetriesServiceImpl implements PoetriesService {
@Resource
private PoetriesDao poetriesDao;
@Override
public void listPoeWithPage(PageModel<Poetries> pageModel){
/*把诗句集合设置给rows*/
pageModel.setRows(poetriesDao.selectPoeListWithPage(pageModel));
/*把总数设置给total*/
pageModel.setTotal(poetriesDao.selectPoeCountWithPage(pageModel));
}
}
PoetsServiceImpl.java
package com.jikexueyuan.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.jikexueyuan.dao.PoetsDao;
import com.jikexueyuan.entity.PageModel;
import com.jikexueyuan.entity.Poets;
import com.jikexueyuan.service.PoetsService;
@Service
public class PoetsServiceImpl implements PoetsService {
@Resource
private PoetsDao poetsDao;
@Override
public void listByName(PageModel<Poets> pageModel) {
pageModel.setRows(poetsDao.findByName(pageModel));
pageModel.setTotal(poetsDao.countWithPage(pageModel));
}
}