特性
/** User 对应的 Mapper 接口 */
public interface UserMapper extends BaseMapper<User> { }
// 初始化 影响行数
int result = 0;
// 初始化 User 对象
User user = new User();
// 插入 User (插入成功会自动回写主键到实体类)
user.setName("Tom");
result = userMapper.insert(user);
// 更新 User
user.setAge(18);
result = userMapper.updateById(user);
// 查询 User
User exampleUser = userMapper.selectById(user.getId());
// 查询姓名为‘张三’的所有用户记录
List<User> userList = userMapper.selectList(
new EntityWrapper<User>().eq("name", "张三")
);
// 删除 User
result = userMapper.deleteById(user.getId());
// 分页查询 10 条姓名为‘张三’的用户记录
List<User> userList = userMapper.selectPage(
new Page<User>(1, 10),
new EntityWrapper<User>().eq("name", "张三")
);
// 分页查询 10 条姓名为‘张三’、性别为男,且年龄在18至50之间的用户记录
List<User> userList = userMapper.selectPage(
new Page<User>(1, 10),
new EntityWrapper<User>().eq("name", "张三")
.eq("sex", 0)
.between("age", "18", "50")
);
SELECT *
FROM sys_user
WHERE (name='张三' AND sex=0 AND age BETWEEN '18' AND '50')
LIMIT 0,10
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描 Xml 文件位置 -->
<property name="mapperLocations" value="classpath:mybatis/*/*.xml"/>
<!-- 配置 Mybatis 配置文件(可无) -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<!-- 配置包别名,支持通配符 * 或者 ; 分割 -->
<property name="typeAliasesPackage" value="com.baomidou.springmvc.model"/>
<!-- 枚举属性配置扫描,支持通配符 * 或者 ; 分割 -->
<property name="typeEnumsPackage" value="com.baomidou.springmvc.entity.*.enums"/>
<!-- 以上配置和传统 Mybatis 一致 -->
<!-- 插件配置 -->
<property name="plugins">
<array>
<!-- 分页插件配置, 参考文档分页插件部分!! -->
<!-- 如需要开启其他插件,可配置于此 -->
</array>
</property>
<!-- MP 全局配置注入 -->
<property name="globalConfig" ref="globalConfig"/>
</bean>
<!-- 定义 MP 全局策略 -->
<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->`mysql`
ORACLE->`oracle`
DB2->`db2`
H2->`h2`
HSQL->`hsql`
SQLITE->`sqlite`
POSTGRE->`postgresql`
SQLSERVER2005->`sqlserver2005`
SQLSERVER->`sqlserver`
-->
<property name="dbType" value="oracle"/>
<!-- 全局表为下划线命名设置 true -->
<property name="dbColumnUnderline" value="true"/>
</bean>
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |