Spring Boot:Spring Boot配置Mybatis-Plus
标签:
SpringBoot
1.首先添加pom文件的依赖:
这仅仅一些所需依赖,不要盲目全直接粘贴到pom文件中。
<!-- 设置版本信息 --> <properties> <mybatis-spring-boot.version>1.1.1</mybatis-spring-boot.version> <mybatisplus-spring-boot-starter.version>1.0.5</mybatisplus-spring-boot-starter.version> <mybatisplus.version>2.0.7</mybatisplus.version> <mysql-connector-java.version>5.1.38</mysql-connector-java.version> <druid.version>1.1.12</druid.version> </properties> <dependencies> <!-- 数据库连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- mybatis 依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-spring-boot.version}</version> </dependency> <!-- jdbc支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java.version}</version> </dependency> <!-- mybatis-plus 依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatisplus-spring-boot-starter</artifactId> <version>${mybatisplus-spring-boot-starter.version}</version> </dependency> <!-- mybatis-plus 依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybatisplus.version}</version> </dependency> </dependencies>
2、修改application.yml
配置文件,添加Mybatis-Plus
的配置
### 启动端口号server: port: 8080### 设置数据库spring: datasource: url: jdbc:mysql://127.0.0.1:3306/hrabbit_admin?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false username: root password: root### 配置mybatisPlusmybatis-plus: mapper-locations: classpath:mapper/*.xml typeAliasesPackage: com.hrabbit.admin.modual.system.bean
3、注入Mybatis-Plus的插件
在数据库配置类,SingleDataSourceConfig.java
中添加如下代码:
import com.alibaba.druid.pool.DruidDataSource;import com.baomidou.mybatisplus.enums.DBType;import com.baomidou.mybatisplus.plugins.PaginationInterceptor;import com.hrabbit.admin.config.properties.DruidProperties;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.transaction.annotation.EnableTransactionManagement;/** * 数据源配置 * * @Auther: hrabbit * @Date: 2018-12-19 11:18 AM * @Description: */@Configuration@EnableTransactionManagement@MapperScan({"com.hrabbit.admin.modual.system.mapper"})public class SingleDataSourceConfig { /** * Druid配置 */ @Bean @ConfigurationProperties(prefix = "spring.datasource") public DruidProperties druidProperties() { return new DruidProperties(); } /** * 单数据源连接池配置 */ @Bean public DruidDataSource dataSource(DruidProperties druidProperties) { DruidDataSource dataSource = new DruidDataSource(); druidProperties.config(dataSource); return dataSource; } /** * mybatis-plus分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); paginationInterceptor.setDialectType(DBType.MYSQL.getDb()); return paginationInterceptor; } }
4、 修改实体Bean
添加@TableName
和@TableId
等注解:
/** * 系统用户 * @Auther: hrabbit * @Date: 2018-12-19 12:02 PM * @Description: */@Data@TableName(value = "sys_user")public class SysUser { /** * 主键id */ @TableId(type = IdType.AUTO) private Integer id; /** * 用户名称 */ private String name; }
5、 修改SysUserMapper.java
主要继承BaseMapper
类,实现一些crud
/** * 系统用户数据库层 * * @Auther: hrabbit * @Date: 2018-12-19 12:02 PM * @Description: */public interface SysUserMapper extends BaseMapper<SysUser> { /** * 根据id查询用户信息 * * @param id * @return */ SysUser findSysUserById(@Param("id") Long id); }
注意 : 如果在xml 中不写SQL的,可以使用注解的方式在此接口当中直接写SQL,实体和数据库表字段不一致,使用@Result注解来映射
6、 修改SysUserService.java
主要继承 IService<T>
/** * 用户业务层 * @Auther: hrabbit * @Date: 2018-12-19 11:59 AM * @Description: */public interface SysUserService extends IService<SysUser> { /** * 根据id获取用户信息 * @param id * @return */ SysUser findSysUserById(Long id); /** * 业务层的根据id获取用户信息 * @param id * @return */ SysUser selectById(Long id); }
7、 修改SysUserServiceImpl.java
/** * 系统用户业务层 * @Auther: hrabbit * @Date: 2018-12-19 12:01 PM * @Description: */@Service@SuppressWarnings("all")public class SysUserServiceImpl extends ServiceImpl<SysUserMapper,SysUser> implements SysUserService { @Autowired private SysUserMapper sysUserMapper; /** * 根据id获取用户信息 * @param id * @return */ @Override public SysUser findSysUserById(Long id){ return sysUserMapper.findSysUserById(id); } /** * 调用mybatis-plus中的根据id查询数据 * @param id * @return */ @Override public SysUser selectById(Long id){ return sysUserMapper.selectById(id); } }
8、测试的Controller
/** * 系统用户 * * @Auther: hrabbit * @Date: 2018-12-17 6:21 PM * @Description: */@Controller@RequestMapping("user")public class SysUserController { @Autowired private SysUserService sysUserService; /** * 根据id获取用户信息 * * @return */ @RequestMapping("/") @ResponseBody public Object index() { return sysUserService.selectById(1L); } }
在Postman访问接口,成功获取到系统用户数据:
image.png
作者:hrabbits
链接:https://www.jianshu.com/p/fbb3bc22c25b
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦