欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第五篇:springboot整合mybatis

程序员文章站 2022-06-17 08:56:08
...

什么是 MyBatis ?

mybatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。Mybatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。Mybatis可以使用简单的XML或注解来配置和映射原生信息,将接口和Java的pojo object映射成数据库中的记录。

创建一个新工程,引入的pom如下:

<?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>

    <groupId>com.wujie</groupId>
    <artifactId>springboot-mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-mybatis</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

配置数据源:

#数据库访问配置
#主数据源,默认的
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/my?useUnicode=true&characterEncoding=utf8&useSSL=false
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    #下面为连接池补充设置,应用到上面所有的数据源中
    #初始化大小
    tomcat:
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 60000  #设置超时时间
      time-between-eviction-runs-millis: 60000   #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      min-evictable-idle-time-millis: 30000         #配置一个链接在池中的最小生存时间
      validation-query: select 'X'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
server:
  port: 80
mybatis:
  mapper-locations: classpath:com/wujie/springbootmybatis/mapper/*.xml

然后使用逆向生成工具帮助我们生成pojo、dao、mapper文件,为我们大大的节约了我们的开发时间。(工具在后面)

创建service

public interface StudentService {
    int deleteByPrimaryKey(Integer id);

    int insertSelective(StudentEntity record);

    StudentEntity selectByPrimaryKey(Integer id);

    List<StudentEntity> findALl();

    int updateByPrimaryKeySelective(StudentEntity record);

}

创建service的实现

 

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return studentDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(StudentEntity record) {
        return studentDao.insertSelective(record);
    }

    @Override
    public StudentEntity selectByPrimaryKey(Integer id) {
        return studentDao.selectByPrimaryKey(id);
    }

    @Override
    public List<StudentEntity> findALl() {
        return studentDao.findAll();
    }

    @Override
    public int updateByPrimaryKeySelective(StudentEntity record) {
        return studentDao.updateByPrimaryKeySelective(record);
    }
}

创建controller

@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @RequestMapping("add")
    public String add(StudentEntity studentEntity){
        Integer i = studentService.insertSelective(studentEntity);
        if (i != 1) {
            return "add fail";
        }else{
            return "add success";
        }
    }

    @RequestMapping("update")
    public String update(StudentEntity studentEntity){
        Integer i = studentService.updateByPrimaryKeySelective(studentEntity);
        if (i != 1) {
            return "update fail";
        }else{
            return "update success";
        }
    }

    @RequestMapping("list")
    public Object list(Integer id){
        if(id != null){
            StudentEntity studentEntity = studentService.selectByPrimaryKey(id);
            if(studentEntity != null){
                return studentEntity;
            }else {
                return " this id is not data";
            }
        }else {
            List<StudentEntity> aLl = studentService.findALl();
            if (aLl != null && aLl.size() > 0) {
                return aLl;
            }else {
                return "data is null";
            }
        }

    }

    @RequestMapping("delete")
    public String delete(Integer id){
        Integer i = studentService.deleteByPrimaryKey(id);
        if (i != 1) {
            return "delete fail";
        }else {
            return "delete success";
        }
    }
}

注意:

我在运行的时候报错了Invalid bound statement (not found): com.wujie.springbootmybatis.dao.StudentDao.findALl,找不到对应的方法,我们可以按照以下步骤一一排除:

  1. 查看我们的对应dao以及mapper是否有错
  2. 查看mapper文件位置是否配置
  3. 查看启动类上是否加了@MapperScan("mapper地址")注解
  4. 查看target中是否有xml文件,如果没有请在pom中添加
<resources>
            <resource>
                <directory>${basedir}/src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>

经过以上步骤,基本上就能够解决,如果还不能解决,请联系我!

欢迎关注我的公众号我们一起学习:

第五篇:springboot整合mybatis

源码地址

mybatis逆向生成工具