目录
Springboot集成MyBatis实现架构:
我的目录结构:
一、导入依赖
<!--MyBatis整合SpringBoot框架的起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--Mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
我的配置文件:
<?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.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wxl</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整合SpringBoot框架的起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--Mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、配置文件
application.yaml
#端口号
server:
port: 8080
#设置链接数据库的配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/admin?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
type-aliases-package: com.wxl.mybatis.pojo # 实体类文件夹路径
mapper-locations: classpath:/mybatis/mapper/*.xml # 资源文件夹下对应的映射xml路径
#设置显示日志,查看sql时用
logging:
file:
name: log/log.log
level:
root: info
com.wxl.mybatis: debug #自己的包名也可不写
三、数据库操作层
package com.wxl.mybatis.dao;
import com.wxl.mybatis.pojo.Person;
import com.wxl.mybatis.pojo.query.PersonQuery;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper //告诉springboot这是一个mapper类
@Repository //将PersonDao交给spring容器管理
public interface PersonDao {
//查询所有
public List<Person> listPerson();
//模糊查询
public List<Person> listPersonByName(String name);
//多参数查询
public List<Person> listPersonByMultiParam(String name, Integer age);
//分页查询
public List<Person> listPersonByPage(PersonQuery personQuery);
public int insertPerson(Person person);
public int updatePersonById(Person person);
public int deletePersonById(int id);
}
这里的@Mapper也可以换种写法,就是不写@Mapper直接到application启动页配置如下:
package com.wxl.mybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.wxl.mybatis.dao")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
使用MapperScan和Mapper注解任意一个即可。
Mapper对应的Mybatis的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.wxl.mybatis.dao.PersonDao">
<select id="listPerson" resultType="com.wxl.mybatis.pojo.Person">
select * from person;
</select>
<select id="listPersonByName" parameterType="String" resultType="com.wxl.mybatis.pojo.Person">
select * from person where name like concat("%",#{name},"%");
</select>
<select id="listPersonByMultiParam" resultType="Person">
select * from person where name like concat("%",#{name},"%")
and age like concat("%",#{age},"%");
</select>
<select id="listPersonByPage" parameterType="com.wxl.mybatis.pojo.query.PersonQuery" resultType="Person">
select * from person limit #{start},#{end};
</select>
<!--useGeneratedKeys="true"表示使用数据库的自动增长策略-->
<insert id="insertPerson" parameterType="Person" useGeneratedKeys="true">
insert into person(name,age,address) values(#{name},#{age},#{address});
</insert>
<update id="updatePersonById" parameterType="Person">
update person set name=#{name},age=#{age},address=#{address} where id=#{id};
</update>
<delete id="deletePersonById" parameterType="int">
delete from person where id=#{id};
</delete>
</mapper>
其中,每个id与dao层接口方法同名,namespace为dao文件的路径。
四、服务层
接口
package com.wxl.mybatis.service;
import com.wxl.mybatis.pojo.Person;
import java.util.List;
public interface PersonService {
public List<Person> listPerson();
public List<Person> listPersonByName(String name);
public List<Person> listPersonByMultiParam(String name, Integer age);
public List<Person> listPersonByPage(Integer pageNum, Integer pageSize);
public int insertPerson(Person person);
public int updatePersonById(Person person);
public int deletePersonById(int id);
}
实现类
package com.wxl.mybatis.service;
import com.wxl.mybatis.dao.PersonDao;
import com.wxl.mybatis.pojo.Person;
import com.wxl.mybatis.pojo.query.PersonQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
PersonDao personDao;
@Override
public List<Person> listPerson() {
return personDao.listPerson();
}
@Override
public List<Person> listPersonByName(String name) {
return personDao.listPersonByName(name);
}
@Override
public List<Person> listPersonByMultiParam(String name, Integer age) {
return personDao.listPersonByMultiParam(name, age);
}
@Override
public List<Person> listPersonByPage(Integer pageNum, Integer pageSize) {
PersonQuery personQuery = new PersonQuery(pageNum, pageSize);
return personDao.listPersonByPage(personQuery);
}
@Override
public int insertPerson(Person person) {
return personDao.insertPerson(person);
}
@Override
public int updatePersonById(Person person) {
return personDao.updatePersonById(person);
}
@Override
public int deletePersonById(int id) {
return personDao.deletePersonById(id);
}
}
五、控制层
package com.wxl.mybatis.controller;
import com.wxl.mybatis.pojo.Person;
import com.wxl.mybatis.service.PersonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class PersonController {
@Autowired
PersonServiceImpl personService;
@RequestMapping("/person")
public List<Person> listPerson(){
return personService.listPerson();
}
@RequestMapping("person2")
public List<Person> listPersonByName(String name){
return personService.listPersonByName(name);
}
@RequestMapping("/person3")
public List<Person> listPersonByMultiParam(String name,Integer age){
return personService.listPersonByMultiParam(name,age);
}
@RequestMapping("/person4")
public List<Person> listPersonByPage(Integer pageNum, Integer pageSize){
return personService.listPersonByPage(pageNum, pageSize);
}
@RequestMapping("/person5")
public String insertPerson(String name,Integer age,String address){
Person person=new Person(name,age,address);
if(personService.insertPerson(person)==1){
return "添加成功";
}else{
return "添加失败";
}
}
@RequestMapping("/person6")
public String updatePerson(){
Person person=new Person(4,"Kite",12,"US");
if(personService.updatePersonById(person)==1){
return "修改成功";
}else{
return "修改失败";
}
}
@RequestMapping("/person7")
public String deletePerson(int id){
if(personService.deletePersonById(id)==1){
return "删除成功";
}else{
return "删除失败";
}
}
}
六、使用注解操作更方便
package com.wxl.redistest.dao;
import com.wxl.redistest.pojo.Person;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface PersonDao {
@Select("select * from person")
List<Person> selectAll();
@Delete("delete from person where id=#{id}")
Integer delete(Integer id);
@Update("update person set name=#{name},age=#{age} where id=#{id}")
Integer update(Person person);
@Insert("insert into person(name,age,address) values(#{name},#{age},#{address})")
Integer insert(Person person);
}