MyBatis-plus有什么特色
1.代码生成
2.条件构造器
快速使用步骤:
1.添加pom文件依赖
<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.0.1</version> </dependency>
2.修改配置文件
将mybatis的sqlSessionFactory替换成mybatis-plus的即可,mybatis-plus只做了一些功能的扩展:
<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.新建一个User表:
@TableName("user") public class User implements Serializable { /** 用户ID */ private Long id; /** 用户名 */ private String name; /** 用户年龄 */ private Integer age; @TableField(exist = false) private String state; }
2.新建Dao层接口UserMapper:
/** * User 表数据库控制层接口 */ public interface UserMapper extends BaseMapper<User> { @Select("selectUserList") List<User> selectUserList(Pagination page,String state); }
3.新建UserMapper配置文件:
<?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.baomidou.springmvc.mapper.system.UserMapper"> <!-- 通用查询结果列--> <sql id="Base_Column_List"> id, name, age </sql> <select id="selectUserList" resultType="User"> SELECT * FROM sys_user WHERE state=#{state} </select> </mapper>
/** * * User 表数据服务层接口实现类 * */ @Service public class UserService extends ServiceImpl<UserMapper, User>{ public Page<User> selectUserPage(Page<User> page, String state) { page.setRecords(baseMapper.selectUserList(page,state)); return page; } }
5,新建controller层UserController
@Controller public class UserController extends BaseController { @Autowired private IUserService userService; @ResponseBody @RequestMapping("/page") public Object selectPage(Model model){ Page page=new Page(1,10); page = userService.selectUserPage(page, "NORMAL"); return page; }
mybatis-plus的条件构建器
首先看一个条件构建器实例的简单实用.
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); }
基本上来说,使用EntityWrapper可以简单地完成一些条件查询,但如果查询方法使用频率很高的话还是建议自己写在UserMapper里.
那么自定义的mapper方法能不能使用EntityWrapper呢,当然也是可以的.
文档中给了一个这样的例子.
1.在Mappper中定义:
List<User> selectMyPage(RowBounds rowBounds, @Param(“ew”) Wrapper<T> wrapper);
2.在mapper文件中定义:
<select id=”selectMyPage” resultType=”User”>
SELECT * FROM user ${ew.sqlSegment}
</select>
对于EntityMapper的条件拼接,基本可以实现sql中常用的where,and,or,groupby,orderby等语法,具体构建方法可以灵活组合.
@Test public void testTSQL11() { /* * 实体带查询使用方法 输出看结果 */ ew.setEntity(new User(1)); ew.where("name={0}", "'zhangsan'").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()); }
转载请注明:前端收藏 » 高效的MyBatis-Plus工具快速入门使用