java类源代码
PagerAspect
package com.rong;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rong.util.PageBean;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Aspect
public class PagerAspect {
@Around("execution(* *..*Service.*Pager(..))")
public Object invoke(ProceedingJoinPoint args) throws Throwable{
Object[] params = args.getArgs();
PageBean pageBean=null;
for (Object param : params) {
if(param instanceof PageBean){
pageBean=(PageBean)param;
break;
}
}
if(pageBean!=null&&pageBean.isPagination()){
PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
}
Object list = args.proceed(params);
if(pageBean!=null&&pageBean.isPagination()){
PageInfo pageInfo=new PageInfo((List) list);
pageBean.setTotal(pageInfo.getTotal()+"");
}
return list;
}
}
mapper包下接口及xml文件
BookMapper
package com.rong.mapper;
import com.rong.model.Book;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
public interface BookMapper {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
List<Map> aaaa(Map map);
}
BookMapper.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.rong.mapper.BookMapper" >
<resultMap id="BaseResultMap" type="com.rong.model.Book" >
<constructor >
<idArg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="bname" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="price" jdbcType="DOUBLE" javaType="java.lang.Double" />
</constructor>
</resultMap>
<sql id="Base_Column_List" >
bid, bname, price
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_books
where bid = #{bid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_books
where bid = #{bid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.rong.model.Book" >
insert into t_books (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=DOUBLE}
)
</insert>
<insert id="insertSelective" parameterType="com.rong.model.Book" >
insert into t_books
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="bid != null" >
bid,
</if>
<if test="bname != null" >
bname,
</if>
<if test="price != null" >
price,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="bid != null" >
#{bid,jdbcType=INTEGER},
</if>
<if test="bname != null" >
#{bname,jdbcType=VARCHAR},
</if>
<if test="price != null" >
#{price,jdbcType=DOUBLE},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.rong.model.Book" >
update t_books
<set >
<if test="bname != null" >
bname = #{bname,jdbcType=VARCHAR},
</if>
<if test="price != null" >
price = #{price,jdbcType=DOUBLE},
</if>
</set>
where bid = #{bid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.rong.model.Book" >
update t_books
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=DOUBLE}
where bid = #{bid,jdbcType=INTEGER}
</update>
<select id="aaaa" resultType="java.util.Map" parameterType="java.util.Map">
select * from t_books
<where>
<if test="null !=bname and bname !=''">
and bname like #{bname}
</if>
</where>
</select>
</mapper>
service接口及方法类
BookService
package com.rong.service;
import com.rong.model.Book;
import com.rong.util.PageBean;
import java.util.List;
import java.util.Map;
public interface BookService {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
List<Map> aaaa(Map map, PageBean pageBean);
List<Map> listPager(Map map, PageBean pageBean);
}
BookServiceimpl
package com.rong.service.impl;
import com.rong.mapper.BookMapper;
import com.rong.model.Book;
import com.rong.service.BookService;
import com.rong.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public int deleteByPrimaryKey(Integer bid) {
return bookMapper.deleteByPrimaryKey(bid);
}
@Override
public int insert(Book record) {
return bookMapper.insert(record);
}
@Override
public int insertSelective(Book record) {
return bookMapper.insertSelective(record);
}
@Override
public Book selectByPrimaryKey(Integer bid) {
return bookMapper.selectByPrimaryKey(bid);
}
@Override
public int updateByPrimaryKeySelective(Book record) {
return bookMapper.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(Book record) {
return bookMapper.updateByPrimaryKey(record);
}
@Override
public List<Map> aaaa(Map map, PageBean pageBean) {
return bookMapper.aaaa(map);
}
@Override
public List<Map> listPager(Map map, PageBean pageBean) {
return bookMapper.aaaa(map);
}
}
Test测试
SpringBaseTest
package com.rong;
import com.rong.model.Book;
import com.rong.util.PageBean;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class SpringBaseTest {
protected Book book;
protected PageBean pageBean;
@Before
public void init(){
book=new Book();
pageBean=new PageBean();
}
}
BookServiceimplTest
package com.rong.service.impl;
import com.rong.SpringBaseTest;
import com.rong.model.Book;
import com.rong.service.BookService;
import com.rong.util.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BookServiceImplTest extends SpringBaseTest {
@Autowired
private BookService bookService;
@Test
public void insert() {
book.setBname("大傻逼");
book.setPrice(18.9);
this.bookService.insert(book);
}
@Test
public void selectByPrimaryKey() {
Book b=this.bookService.selectByPrimaryKey(3);
System.out.println(b);
}
@Test
public void aaaa() {
Map map=new HashMap();
map.put("bname", StringUtils.toLikeStr("斗罗"));
List<Map> aaaa = this.bookService.aaaa(map, pageBean);
for (Map m : aaaa) {
System.out.println(m);
}
}
@Test
public void listPager() {
Map map=new HashMap();
map.put("bname", StringUtils.toLikeStr("斗罗"));
pageBean.setPage(1);
List<Map> aaaa = this.bookService.aaaa(map, pageBean);
for (Map m : aaaa) {
System.out.println(m);
}
}
}