码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatisPlus使用

时间:2021-06-02 17:32:39      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:alibaba   druid   ike   generator   repo   llb   tco   timezone   targe   

1 简介

MyBatis-plus 是一款 Mybatis 增强工具,用于简化开发,提高效率。下文使用缩写 mp来简化表示 MyBatis-plus,本文主要介绍 mp 搭配 Spring Boot 的使用

官方网站:https://mp.baomidou.com/

1.1 项目搭建

创建一个Spring Boot项目,本教程是 mp 搭配 Spring Boot 使用的教程

1.1.1 maven依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
	</parent>
	<dependencies>
		<!-- springboot启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.4.2</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>

		<!-- druid连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.9</version>
		</dependency>
		<!-- 反向生成 -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-generator</artifactId>
			<version>RELEASE</version>
		</dependency>
		<dependency>
			<groupId>com.jslsolucoes</groupId>
			<artifactId>ojdbc6</artifactId>
			<version>11.2.0.1.0</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.apache.velocity</groupId>
			<artifactId>velocity-engine-core</artifactId>
			<version>2.0</version>
		</dependency>




		<!-- 序列化对象 -->
		<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.75</version>
		</dependency>


	</dependencies>

1.1.2 配置文件部分

#配置springboot的项目名字
spring.application.name=项目测试名字
#配置端口号
server.port=8080
#指定访问项目路径的项目名
server.servlet.context-path=/mp

#配置mysql的驱动连接
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root

#连接类型说明为druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#允许使用列标签代替别名,例如:select user_id as userId from user 是使用userId代替user_id 
#mybatis.configuration.use-column-label=true
#扫描mapper.xml文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#开启驼峰命名的意思是查询出来的对象里面的有下划线的字段自动转换为驼峰命名形式,并和实体去匹配
mybatis.configuration.map-underscore-to-camel-case=true
#显示mybatis-plus的sql
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#日志级别
logging.level.org.springframework=info

1.1.3 创建实体

@TableName("user_info")
public class UserInfoEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	@TableId(value = "user_id", type = IdType.AUTO)
	private Long userId;

	@TableField("user_name")
	private String userName;

	@TableField("user_age")
	private Integer userAge;

	@TableField("user_address")
	private String userAddress;

	@TableField("user_account")
	private String userAccount;

	@TableField("user_password")
	private String userPassword;
	//省去getset方法
	}

1.1.4 创建mapper

mapper接口

public interface UserInfoMapper extends BaseMapper<UserInfoEntity> {

	List<UserInfoEntity> queryAllBySelf();
}

mapper.xml

<?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="cn.jzh.mapper.UserInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.jzh.entity.UserInfoEntity">
        <id column="user_id" property="userId" />
        <result column="user_name" property="userName" />
        <result column="user_age" property="userAge" />
        <result column="user_address" property="userAddress" />
        <result column="user_account" property="userAccount" />
        <result column="user_password" property="userPassword" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        user_id, user_name, user_age, user_address, user_account, user_password
    </sql>
    
    <select id="queryAllBySelf" resultMap="BaseResultMap">
    	select * from user_info
    </select>

</mapper>

1.1.5 创建service

service接口

public interface UserInfoService extends IService<UserInfoEntity> {

	List<UserInfoEntity> queryAllBySelf();
}

service接口实现类

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfoEntity> implements UserInfoService {

	@Autowired
	UserInfoMapper mapper;
	
	
	@Override
	public List<UserInfoEntity> queryAllBySelf() {
		// TODO Auto-generated method stub
		return mapper.queryAllBySelf(); 
	}
}

1.1.6 创建controller

@RestController
public class UserInfoController {

	@Autowired
	private UserInfoService service;

	// 查询全部
	@RequestMapping("/queryAll")
	public List<UserInfoEntity> queryAll() {
		List<UserInfoEntity> list = service.list();
		return list;
	}
	
	@RequestMapping("/queryAllBySelf")
	public List<UserInfoEntity> queryAllBySelf() {
		List<UserInfoEntity> list = service.queryAllBySelf();
		return list;
	}

	// 查询部分列
	@RequestMapping("/queryPartCols")
	public List<UserInfoEntity> queryPartCols() {
		QueryWrapper<UserInfoEntity> wrapper = new QueryWrapper<>();
	    wrapper.select("user_id","user_name","user_age").likeRight("user_name","黄");
	    List<UserInfoEntity> list = service.list(wrapper);
		return list;
	}

	// 查询 objs
	@RequestMapping("/queryFirstColumn")
	public List<Object> queryFirstColumn() {
		
		List<Object> list = service.listObjs();
		return list;
	}

	// 查询全部
	@RequestMapping("/queryCounts")
	public int queryCounts() {
		QueryWrapper<UserInfoEntity> wrapper = new QueryWrapper<>();
		  wrapper.like("user_name", "四");
		int count = service.count(wrapper);
		return count;
	}

	//查询单个
	@RequestMapping("/getOne")
	public UserInfoEntity getOne() {
		LambdaQueryWrapper<UserInfoEntity> wrapper = Wrappers.<UserInfoEntity>lambdaQuery();
	     wrapper.gt(UserInfoEntity::getUserAge, 28);
		UserInfoEntity one = service.getOne(wrapper, false);
		
		
		return one;
	}

	// 修改
	@RequestMapping("/updateByUser")
	public boolean updateByUser() {
		UpdateWrapper<UserInfoEntity> wrapper = new UpdateWrapper<>();
		wrapper.eq("user_id", 2);
		UserInfoEntity entity = new UserInfoEntity();
		entity.setUserName("李思思");
		boolean update = service.update(entity, wrapper);
		return update;
	}
}

MyBatisPlus使用

标签:alibaba   druid   ike   generator   repo   llb   tco   timezone   targe   

原文地址:https://www.cnblogs.com/jingzh/p/14828400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!