SpringBoot 结合mybatis-plus整理
一.新建一个SpringBoot
构建准备
- jdk 1.8 或以上
- maven
- IntelliJ IDEA
打开Idea-> new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步就行了。
创建完工程,工程的目录结构如下:
- pom文件为基本的依赖管理文件
- resouces 资源文件
- statics 静态资源
- templates 模板资源
- application.properties 配置文件(后面更换成.yml)
- SpringbootApplication程序的入口
POM文件配置参考
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.springboot</groupId> <artifactId>mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatis</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <mybatis-plus.version>3.0.6</mybatis-plus.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- 模板引擎 代码生成 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
创建Controller
@RestController public class HelloWordController { @RequestMapping("/") public String index() { return "Hello This is Spring Boot Test!"; } }
启动项目,在浏览器中输入: http://localhost:8080/
可以看到输出
一.集成建一个mybatis-plus
1)pom新增配置
<!-- mybatisplus与springboot整合 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatisplus-spring-boot-starter</artifactId> <version>1.0.5</version> </dependency> <!-- MP 核心库 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>2.1.8</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- 模板引擎 代码生成 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency> <!-- connection pool --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
2)修改application.yml
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test20191031 username: root password: 123456 #mybatis mybatis-plus: mapper-locations: classpath*:mapper/**/*.xml #实体扫描,多个package用逗号或者分号分隔 typeAliasesPackage: com.honghh.bootfirst.entity global-config: #主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID"; id-type: 0 #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断" field-strategy: 2 #驼峰下划线转换 db-column-underline: true #刷新mapper 调试神器 refresh-mapper: true #数据库大写下划线转换 #capital-mode: true # Sequence序列接口实现类配置 #key-generator: com.baomidou.mybatisplus.incrementer.OracleKeyGenerator #逻辑删除配置 #logic-delete-value: -1 #logic-not-delete-value: 0 #自定义填充策略接口实现 #meta-object-handler: com.baomidou.springboot.xxx #自定义SQL注入器 sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector configuration: map-underscore-to-camel-case: true cache-enabled: false call-setters-on-nulls: true
3)新增Mapper及xml配置,结构如下(如能力强可参考自动生成)
/** * <p> * Mapper 接口 * </p> * * @author WUql123 * @since 2019-11-01 */ public interface MyInfoMapper extends BaseMapper<MyInfo> { }
MyInfoMapper.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="com.springboot.mybatis.mapper.MyInfoMapper"> </mapper>
4)映射数据库实体类
public5 class MyInfo implements Serializable { private static final long serialVersionUID = 1L; @TableId(value = "id", type = IdType.AUTO) private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "MyInfo{" + "id=" + id + ", name=" + name + ", age=" + age + "}"; } }
5)新建接口服务类 extends (com.springboot.mybatis.service)IService
@Service public interface MyInfoService extends IService<MyInfo> { }
服务实现类
@Service public class MyInfoServiceImpl extends ServiceImpl<MyInfoMapper, MyInfo> implements MyInfoService { }
6.新建一个控制类用于测试
@Controller @RequestMapping("/myInfo") public class MyInfoController { @Resource MyInfoService myInfoService; @GetMapping("/getInfo") public String myInfo(@RequestParam Integer id) { MyInfo myInfo = myInfoService.selectById(id); System.out.println(myInfo.toString()); return myInfo.toString(); } }
7.浏览器测试
http://localhost:8080/myInfo/getInfo?id=1
结果输出
MyInfo{id=1, name=lus, age=18}
则完成一个简单的查询
注:例子涉及到的表结构
CREATE TABLE `my_info` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;