SpringBoot_集成MyBatis(XML方式)
2017-03-31 本文已影响3582人
perfect_jimmy
1.pom.xml配置
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<!-- 请不要使用1.0.0版本,因为还不支持拦截器插件,使用最新版本即可 -->
<version>1.0.1-SNAPSHOT</version>
</dependency>
Paste_Image.png
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");
//通过设置pageSize=0或者RowBounds.limit = 0就会查询出全部的结果。
p.setProperty("pageSizeZero", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
3.使用:在查询方法前面调用startPage
//第一个参数:第几页
//第二个参数:每页显示条数
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");//分页数
System.out.println(page.getTotal()+"-============total");//总数