1.pom.xml配置
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<!-- 請不要使用1.0.0版本斑粱,因為還不支持?jǐn)r截器插件,使用最新版本即可 -->
<version>1.0.1-SNAPSHOT</version>
</dependency>
2.application.properties
mybatis.type-aliases-package:com.chinac.ccs.mirror.pojo
mybatis.mapper-locations:classpath:/com/chinac/ccs/mirror/mapper/mapper*.xml
或者:
#mybatis配置
#Mapper.xml所在的位置
mybatis.mapper-locations=classpath*:mappers/*Mapper.xml
#entity掃描的包名
mybatis.type-aliases-package=com.example.entity
3.實體類
public class Mirror implements Serializable{
private long id;
private String name;
//省略getter禽作、setter方法
}
4.mapper映射:方法名字要和映射文件中的ID一致棕硫,這樣就可以自動匹配
- dao接口:
@Mapper
public interface MirrorDao {
public void add(Mirror mirror);
public Mirror findById(@Param(value="id")long id);
public void delete(@Param(value="id")long id);
public void update(Mirror mirror);
public List<Mirror> list(@Param(value="name")String name);
}
- 映射文件:
<?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.chinac.ccs.mirror.dao.MirrorDao">
<resultMap type="com.chinac.ccs.mirror.pojo.Mirror" id="BaseResultMap">
<id column="id" property="id" />
<result column="name" property="name" />
</resultMap>
<select id="findById" resultMap="BaseResultMap">
SELECT * FROM mirror WHERE id = #{id}
</select>
<insert id="add" parameterType="com.chinac.ccs.mirror.pojo.Mirror"
keyProperty="id" useGeneratedKeys="true">
insert into mirror
<trim prefix="(" suffix=")" suffixOverrides=",">
id,name,
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
#{id,jdbcType=INTEGER},#{name,jdbcType=VARCHAR},
</trim>
</insert>
<select id="delete">
delete from mirror where 1=1
<if test="id != null">
and id = #{id}
</if>
</select>
<update id="update" parameterType="com.chinac.ccs.mirror.pojo.Mirror">
update mirror
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
</set>
where id = #{id}
</update>
<select id="list" resultMap="BaseResultMap">
select * from mirror where 1=1
<if test="name != null ">
and <![CDATA[name like CONCAT('%',#{name,jdbcType=VARCHAR},'%' )]]>
</if>
</select>
</mapper>
5.service
@Service
public class MirrorService {
@Autowired
private MirrorDao mirrorDao;
public Mirror findById(Long id){
return mirrorDao.findById(id);
}
}
pageHelper分頁插件
1.pom.xml配置
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>
2.添加配置類
@Configuration
public class MyBatisConfiguration{
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
//通過設(shè)置pageSize=0或者RowBounds.limit = 0就會查詢出全部的結(jié)果螟加。
p.setProperty("pageSizeZero", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
3.使用:在查詢方法前面調(diào)用startPage
//第一個參數(shù):第幾頁
//第二個參數(shù):每頁顯示條數(shù)
Page<Mirror> obj = PageHelper.startPage(1,2);
List<Mirror> list = mirrorService.list("Jimmy");
System.out.println(list.size()+"-------------------");
PageInfo<Mirror> page = new PageInfo<Mirror>(list);
System.out.println(page.getSize()+"-=============");//2
System.out.println(page.getPages()+"=========pages");//分頁數(shù)
System.out.println(page.getTotal()+"-============total");//總數(shù)