前言
根据上一个博客的后继书写,上一个博客地址springboot使用一:springboot整合mybatis,使用逆向工程生成java代码。
上一个博客仅仅是使用mybatis generator生成SQL和java模型代码,但是却没有开发环境。这里就写一下流程,对maven的依赖更加熟练一下。
代码也是根据基于上一个博客的代码进行开发。都是很简单的demo。可以直接复习看。
代码在https://github.com/fengfanli/springboot-mybatis 这儿,再分支
dev01中。
一、pom依赖
1. 新增maven依赖
<!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--数据源--> <!--http://localhost:8083/druid/index.html 通过这个网址 对 SQL进行监控--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> 2. 全部maven依赖
<?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.4.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.feng</groupId> <artifactId>springboot-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-mybatis</name> <description>springboot-mybatis</description> <properties> <java.version>1.8</java.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-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <!-- mybatis逆向工程jar包:mybatis-generator-core --> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-maven-plugin --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> </dependency> <!--逆向工程,自动生成@Table、@Id等注解,会使用到下面两个注解--> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> <!--通用Mapper启动器--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.3</version> </dependency> <!--第一次新增加:mysql驱动和Druid数据源--> <!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--数据源--> <!--http://localhost:8083/druid/index.html 通过这个网址 对 SQL进行监控--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> </dependencies> <build> <finalName>springboot-mybatis</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <overwrite>true</overwrite> <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile> <!--避免操作不当,覆盖原来的类,设置为不覆盖:false--> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--<dependency> <groupId>com.thunisoft.arterybase</groupId> <artifactId>ArteryBase</artifactId> <version>3.6.2.2</version> </dependency>--> <!--自动生成@Table、@Id等注解--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>4.0.0</version> </dependency> </dependencies> </plugin> </plugins> </build> </project> 二、yml配置
1. 配置数据源
server: port: 8086 spring: application: name: company-frame datasource: type: com.alibaba.druid.pool.DruidDataSource druid: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.131.168:3306/CLASS?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: Dataadt123! mybatis: # 配置映射类所在的包名 type-aliases-package: com.feng.bean # 配置 mapper xml 文件所在的路径, 如果不配置 会报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) mapper-locations: classpath:mapper/*.xml 三、修改启动主类
package com.feng; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan(value = {"com.feng.dao"}) //@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) @SpringBootApplication public class SpringbootMybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringbootMybatisApplication.class, args); } } 四、java代码
1. controller 控制层
package com.feng.controller; import com.feng.bean.Student; import com.feng.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class StudentController { @Autowired private StudentService studentService; @RequestMapping("/stu/{userId}") public Student getStuByIdPath(@PathVariable Integer userId){ Student student = studentService.getStuById(userId); return student; } @GetMapping(value = "/stu") @ResponseBody public DataResult<Student> getCompanyByIdGET(@RequestParam(value = "userId") Integer id) { Student student = studentService.getStuById(id); DataResult result = DataResult.success(student); return result; } } 2. service业务层
a. StudentService 接口
package com.feng.service; import com.feng.bean.Student; import com.feng.vo.req.StudentPageReqVo; import com.feng.vo.resp.PageRespVo; import java.util.List; public interface StudentService { Student getStuById(Integer userId); } b. StudentServiceImpl 实现类
impl包下的StudentServiceImpl.java 实现类
package com.feng.service.impl; import com.feng.bean.Student; import com.feng.dao.StudentMapper; import com.feng.service.StudentService; import com.feng.utils.PageUtil; import com.feng.vo.req.StudentPageReqVo; import com.feng.vo.resp.PageRespVo; import com.github.pagehelper.PageHelper; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class StudentServiceImpl implements StudentService { @Resource private StudentMapper studentMapper; @Override public Student getStuById(Integer userId) { return studentMapper.selectByPrimaryKey(userId); } } 3. dao数据层
这里不在复制了,dao层为接口,mapper为SQL语句,都为mybatis逆向生成的。GitHub中可查看。
五、postman请求测试
1. 请求/stu 方式一
http://localhost:8086/stu/1
2. 请求/stu 方式二
http://localhost:8086/stu?userId=1
到现在为止,springboot整合mybatis generator、mybatis,开发环境,已经可以啦