目录
一、环境
springboot:2.7.3
jdk:1.8
idea:2021.3
mysql:8.0.29
完整代码链接:https://pan.baidu.com/s/1Xzj6yN_dy5a0nfTYhSs82Q?pwd=rn68
提取码:rn68
二、前期准备
1.新建module,选好依赖,这里选择spring web、mybatis和mysql依赖,在pom.xml中查看结果。
2.继续在pom.xm中添加 mybatis代码自动生成插件和处理资源目录(使得resources文件夹下文件即mapper文件生效), mybatis代码自动生成的文件GeneratorMapper.xml这里须在根目录下,具体文件内容见链接。
<!--各种依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--处理资源目录-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<!--mybatis代码自动生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.Mysql数据库的创建,使用Navicat简单创建即可,名为springdb,包含id、name、age字段。
4.配置好application.preperties
#端口
server.port=9002
#context-path
server.servlet.context-path=/mytrans
#连接数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
#指定mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
#mybatis目录
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
三、逆向生成
1.对逆向生成文件GeneratorMapper.xml编译。首先是指定好数据库JDBC位置,配置好数据库 。再对生成的各个文件编译,包括model类、具体sql语句的mapper.xml以及Dao接口,最后根据自己建的数据库,设置数据库表名和java模型类名(注意大小写)。
2.配置好逆向生成文件,然后找到Maven菜单栏,点击指定按钮。
3.运行结果:说明逆向生成完成,同时
四、业务逻辑及测试
1.service层:使用Dao里的新增功能
StudentService.interface:
package com.bjpowernode.service;
import com.bjpowernode.model.Student;
public interface StudentService {
int addStudent(Student student);
}
StudentServiceImpl:代码中涉及事务,这里不用管不影响逻辑业务
package com.bjpowernode.service.impl;
import com.bjpowernode.dao.StudentMapper;
import com.bjpowernode.model.Student;
import com.bjpowernode.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentMapper studentDao;
/**
* @Transactional: 表示方法的有事务支持
* 默认:使用库的隔离级别, REQUIRED 传播行为; 超时时间 -1
* 抛出运行时异常,回滚事务
*/
@Transactional
@Override
public int addStudent(Student student) {
System.out.println("业务方法addStudent");
int rows = studentDao.insert(student);
System.out.println("执行sql语句");
//抛出一个运行时异常, 目的是回滚事务
// int m = 10 / 0 ;
return rows;
}
}
2.controller:编写新增学生的逻辑,
package com.bjpowernode.controller;
import com.bjpowernode.model.Student;
import com.bjpowernode.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
public class StudentController {
@Resource
private StudentService service;
@RequestMapping("/addStudent")
@ResponseBody
public String addStudent(String name,Integer age){
Student s1 = new Student();
s1.setName(name);
s1.setAge(age);
int rows = service.addStudent(s1);
return "添加学生:"+rows;
}
}
运行文件:
package com.bjpowernode;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @EnableTransactionManagement: 启用事务管理器
*/
@SpringBootApplication
@EnableTransactionManagement
@MapperScan(basePackages = "com.bjpowernode.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
结果:
五、bug解决
1.问题:添加数据失败 id为null
解决:设置id主键自增
2.问题:启动不了springboot程序,同时不生成逆向文件
解决方法1:
- 在GeneratorMapper.xml文件中的<jdbcConnection></jdbcConnection>中加入<property name="nullCatalogMeansCurrent" value="true"/>
解决方法2:更管用!!
先把解决方法1添加进去,然后在Navicat修改student表名为student22,删除原先生成的dao等文件,重新执行一下逆向工程文件没反应,再改回表名为student,再执行逆向工程文件,成功修改。