我们上一节给大家讲了springboot2结合mybatis实现mysql数据的增删改查,但是是要用到xml配置的,一旦涉及到xml配置,就会比较麻烦。今天再来给大家讲一个新的方法,不用设置xml文件,并且代码看起来更简洁。
一,引入mybatis和数据库连接的依赖
完整的pom.xml贴出来给大家
<?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 http://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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.shitou</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>
</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>
</dependency>
<!-- mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>LATEST</version>
</dependency>
<!-- 数据库链接依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二,配置数据库连接
这里我的数据库是用的mysql数据库,使用的是test2库。
三,建库建表
创建test2数据库
create database test2 default character set utf8 collate utf8_general_ci;
创建user2数据表
CREATE TABLE user2(
id int(32) auto_increment NOT NULL PRIMARY KEY,
name VARCHAR(32) comment '姓名' NOT NULL,
age VARCHAR(32) comment '年纪' NOT NULL
) COMMENT '用户表' CHARACTER SET utf8 COLLATE utf8_general_ci;
四,创建与数据表对应的实体类
五,创建操作数据库的mapper
代码给大家贴出来
@Mapper
public interface User2Mapper {
@Insert("insert into user2(name, age) values(#{name}, #{age})")
int add(@Param("name") String name, @Param("age") int age);
@Update("update user2 set name = #{name}, age = #{age} where id = #{id}")
int update(@Param("name") String name, @Param("age") int age, @Param("id") int id);
@Delete("delete from user2 where id = #{id}")
int delete(int id);
@Select("select id, name as name, age as age from user2 where id = #{id}")
User2 findOne(@Param("id") int id);
@Select("select * from user2")
List<User2> findAll();
}
六,创建service和controller
service代码如下
@Service
public class User2Service {
@Resource
private User2Mapper accountMapper;
public int add(String name, int age) {
return accountMapper.add(name, age);
}
public int update(String name, int age, int id) {
return accountMapper.update(name, age, id);
}
public int delete(int id) {
return accountMapper.delete(id);
}
public User2 findAccount(int id) {
return accountMapper.findOne(id);
}
public List<User2> findAccountList() {
return accountMapper.findAll();
}
}
controller代码如下
@RestController
@RequestMapping("/mybatis")
public class User2Controller {
@Resource
User2Service accountService;
@GetMapping("/list")
public List<User2> getAccounts() {
return accountService.findAccountList();
}
@GetMapping("/findone")
public User2 getAccountById(@RequestParam("id") int id) {
return accountService.findAccount(id);
}
@GetMapping("/update")
public String updateAccount(@RequestParam("id") int id,
@RequestParam(value = "name") String name,
@RequestParam(value = "age") int age) {
int t = accountService.update(name, age, id);
if (t == 1) {
return "success";
} else {
return "fail";
}
}
@GetMapping("/delete")
public String delete(@RequestParam(value = "id") int id) {
int t = accountService.delete(id);
if (t == 1) {
return "success";
} else {
return "fail";
}
}
@GetMapping("/add")
public String postAccount(@RequestParam(value = "name") String name,
@RequestParam(value = "age") int age) {
int t = accountService.add(name, age);
if (t == 1) {
return "success";
} else {
return "fail";
}
}
}
七,启动项目,做验证
启动springboot项目
添加数据。如下图,我们通过add添加两个数据
通过list 查询所有数据
更新和删除数据,就不给大家演示了。
网友评论