1.在eclipse裏面添加pom.xml的配置
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.6</version>
</dependency>
注意:mybatis-plus會自動維護mybatis以及mybatis-spring的依賴,所以不需要引入後兩者,避免發生版本衝突
2.修改配置文件
在spring配置文件中application-dao.xml中,將mybatis的配置替換爲
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自動掃描Mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mybatis/*/*.xml"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<property name="typeAliasesPackage" value="com.baomidou.springmvc.model.*"/>
<property name="plugins">
<array>
<!-- 分頁插件配置 -->
<bean id="paginationInterceptor" class="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
<property name="dialectType" value="mysql"/>
</bean>
</array>
</property>
<!-- 全局配置注入 -->
<property name="globalConfig" ref="globalConfig" />
</bean>
<bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!--
AUTO->`0`("數據庫ID自增")
INPUT->`1`(用戶輸入ID")
ID_WORKER->`2`("全局唯一ID")
UUID->`3`("全局唯一ID")
-->
<property name="idType" value="2" />
<!--
MYSQL->`mysql`
ORACLE->`oracle`
DB2->`db2`
H2->`h2`
HSQL->`hsql`
SQLITE->`sqlite`
POSTGRE->`postgresql`
SQLSERVER2005->`sqlserver2005`
SQLSERVER->`sqlserver`
-->
<!-- Oracle需要添加該項 -->
<!-- <property name="dbType" value="oracle" /> -->
<!-- 全局表爲下劃線命名設置 true -->
<property name="dbColumnUnderline" value="true" />
</bean>
開始使用1.寫實體
/*
* 這裏有兩個註解需要注意,第一是@tableName("user"),它是指定與數據庫表的關聯,這裏的註解意味着你的數據庫裏應該有一個名爲user的
*表與之對應,
* 並且數據表的列名應該就是User類的屬性,對於User類中有而user表中沒有的屬性需要加第二個註解@TableField(exist = false),
* 表示排除User類中的屬性.
*/
@TableName("Items")
public class Items implements Serializable{
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
@TableField(exist = false)
private String test;
}
2.寫dao接口
/**
* mybatisplus dao接口
* @author LY
*
*/
public class itemsExtMapper extends BaseMapper<Items>{
@Select("selectItemsList")
List<Items> selectItemsList(Pagination page,String state);
}
3.新建Mapper文件
<?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="gz.kd.ItemsExtMapper">
<!-- 通用查詢結果列-->
<sql id="Base_Column_List">
id, name, age
</sql>
<select id="selectItemsList" resultType="Items">
SELECT * FROM sys_user WHERE state=#{state}
</select>
</mapper>
4.新建service層
/**
* ItemsExtService繼承了ServiceImpl類,mybatis-plus通過這種方式爲我們注入了itemsExtMapper,
* 樣可以使用service層默認爲我們提供的很多方法,也可以調用我們自己在dao層編寫的操作數據庫的方法.
* Page類是mybatis-plus提供分頁功能的一個model,繼承了Pagination,這樣我們也不需要自己再編寫一個Page類,
* 直接使用即可.
* @author LY
*
*/
public class ItemsExtService extends ServiceImpl<itemsExtMapper, Items>{
public Page<Items> selectItemsPage(Page<Items> page, String state) {
page.setRecords(baseMapper.selectItemsList(page,state));
return page;
}
}
5.controller中的使用
Page page=new Page(1,10);
page = itemsService.selectItemsPage(page, "NORMAL");
Mybatisplus的條件構造器
使用entityWrapper可以完成一些簡單的條件查詢
public void test(){
EntityWrapper ew=new EntityWrapper();
ew.setEntity(new User());
String name="wang";
Integer age=16;
ew.where("name = {0}",name).andNew("age > {0}",age).orderBy("age");
List<User> list = userService.selectList(ew);
Page page2 = userService.selectPage(page, ew);
}
自定義的mapper同樣能後使用EntityWrapper1.在Mappper中定義:
List<Items> selectMyPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper);
2.在mapper文件中定義:<select id="selectMyPage" resultType="Items">
SELECT * FROM items ${ew.sqlSegment}
</select>
@Test
public void testTSQL11() {
/*
* 實體帶查詢使用方法 輸出看結果
*/
ew.setEntity(new Items(1));
ew.where("pic={0}", "'kd.jpg'").and("id=1")
.orNew("status={0}", "0").or("status=1")
.notLike("nlike", "notvalue")
.andNew("new=xx").like("hhh", "ddd")
.andNew("pwd=11").isNotNull("n1,n2").isNull("n3")
.groupBy("x1").groupBy("x2,x3")
.having("x1=11").having("x3=433")
.orderBy("dd").orderBy("d1,d2");
System.out.println(ew.getSqlSegment());
}