使用Spring Boot整合MyBatis进行数据库操作
pom文件所需依赖
<dependencies>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
<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>3.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
</dependencies>
spring-boot-starter:Spring Boot的核心启动器,包含了Spring Boot的一些基本依赖,比如配置文件、日志、Spring MVC、Tomcat等。
spring-boot-starter-web:Spring Boot Web应用启动器,包含了Spring MVC、Tomcat、Jackson等依赖,用于快速搭建一个Web应用。
spring-boot-starter-test:Spring Boot测试支持库,包含了JUnit、Spring Test、Mockito等依赖,用于测试Spring Boot应用。
pagehelper-spring-boot-starter:pagehelper-spring-boot-starter是一个基于Spring Boot的分页插件PageHelper的Starter依赖。
mybatis-spring-boot-starter:mybatis-spring-boot-starter是一个用于在Spring Boot应用程序中集成MyBatis的依赖。它包括MyBatis框架本身以及一些与Spring Boot集成相关的配置类和自动配置类。使用该依赖可以快速、简便地在Spring Boot应用程序中使用MyBatis进行持久化操作。
mysql-connector-java:mysql-connector-java是MySQL提供的官方Java语言操作MySQL数据库的驱动程序,也是Java平台上最常用的MySQL数据库驱动之一。在Spring Boot应用程序中,我们通常需要通过mysql-connector-java依赖来连接MySQL数据库并进行数据操作。
lombok:Lombok是一种Java的插件,可以通过自动生成代码来减少Java开发中的代码量,使Java代码更加简洁、易读。它提供了许多注解,可以用来自动生成getter、setter、构造函数、equals、hashCode和toString等方法,从而简化Java类的编写。
建立基本框架
entity(实体类层)
controller(web层)
service(业务层)
mapper(数据访问层)
数据增删改查
实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private String address;
}
查询(分页模糊)
controller层
/**
* 自定义PageInfo进行分页模糊查询
* @param pageNo
* @param pageSize
* @param searchValue
* @return
*/
@RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
public PageInfoUtils<Address> selectByPage(
@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
@RequestParam("searchValue") String searchValue){
PageHelper.startPage(pageNo,pageSize);
List<Address> addressList = this.addressService.getListByPage(searchValue);
PageInfoUtils<Address> pageInfoUtils = new PageInfoUtils<>(addressList);
return pageInfoUtils;
}
service层
- service接口
/**
* pageHelper分页模糊查询
* @param searchValue
* @return
*/
List<Address> getListByPage(String searchValue);
- service接口实现类
@Override
public List<Address> getListByPage(String searchValue) {
List<Address> listByPage = this.addressMapper.getListByPage(searchValue);
return listByPage;
}
mapper层
- mapper接口
/**
* 分页模糊查询
* @param searchValue
* @return
*/
List<Address> getListByPage(String searchValue);
- mapper.xml
<select id="getListByPage" resultType="com.lee.testaddress.entity.Address">
select
id, name, address
from
t_address
<where>
<if test="searchValue != null and searchValue != ''">
and name like concat ('%',#{searchValue},'%')
</if>
</where>
</select>
通过apifox请求
{
"total": 40,
"list": [
{
"id": 1,
"name": "zhangsan",
"address": "Beijing"
},
{
"id": 2,
"name": "zhangsan",
"address": "Beijing"
},
{
"id": 3,
"name": "zhangsan",
"address": "Beijing"
},
{
"id": 4,
"name": "zhangsan",
"address": "Beijing"
},
{
"id": 5,
"name": "lisi",
"address": "Shanghai"
},
{
"id": 6,
"name": "zhangsan",
"address": "Beijing"
},
{
"id": 7,
"name": "zhangsan",
"address": "Beijing"
},
{
"id": 8,
"name": "zhangsan",
"address": "Beijing"
},
{
"id": 9,
"name": "zhangsan",
"address": "Beijing"
},
{
"id": 10,
"name": "wangwu",
"address": "Shenzhen"
}
],
"pageNum": 1,
"pageSize": 10,
"pages": 4
}
新增或修改
controller层
/**
* 新增或修改
* @param address
*/
@PostMapping("/edit")
public void edit(@RequestBody Address address){
if (address.getId() == null){
this.addressService.insert(address);
}
this.addressService.updateAddress(address);
}
service层
- service接口
/**
* 添加单条数据
* @param address
* @return
*/
void insert(Address address);
/**
* 修改单条数据
* @param address
* @return
*/
void updateAddress(Address address);
- service接口实体类
@Override
@Transactional(rollbackFor = Exception.class )
public void insert(Address address) {
this.addressMapper.insert(address);
}
@Override
@Transactional(rollbackFor = Exception.class )
public void updateAddress(Address address) {
this.addressMapper.updateById(address);
}
删除
controller层
/**
* 删除单条数据
* @param id
*/
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable ("id") Integer id){
this.addressService.delete(id);
}
service层
- service接口
/**
* 删除单条数据
* @param id
*/
void delete(Integer id);
- service接口实体类
@Override
@Transactional(rollbackFor = Exception.class )
public void delete(Integer id) {
this.addressMapper.deleteById(id);
}
这里使用的新增,修改和删除我是用的是mybatis-plus的通用mapper方法,所以没有写sql语句。其实和查询相似,只是sql语句不同,都是一些简单的增删改,所以我没在文章中写明。
在实际应用当中,我个人感觉重要的是service层和mapper也就是sql语句的书写这两块是最重要的,更多的还是要学习sql语句和更高级sql语句的写法,有时候一条sql就能解决较复杂的需求。
学习不易,还是要多多理解,多多坚持,加油!