一、项目创建

  • 新建一个工程

SpringBoot整合Mybatis【非注解版】-LMLPHPSpringBoot整合Mybatis【非注解版】-LMLPHP

  • 选择Spring Initializr,配置JDK版本

SpringBoot整合Mybatis【非注解版】-LMLPHPSpringBoot整合Mybatis【非注解版】-LMLPHP

  • 输入项目名

SpringBoot整合Mybatis【非注解版】-LMLPHPSpringBoot整合Mybatis【非注解版】-LMLPHP

  • 选择构建web项目所需的staters(启动器)

SpringBoot整合Mybatis【非注解版】-LMLPHPSpringBoot整合Mybatis【非注解版】-LMLPHP


  • 选择与数据库相关的组件

SpringBoot整合Mybatis【非注解版】-LMLPHPSpringBoot整合Mybatis【非注解版】-LMLPHP


  • 选择保存地址后点击Finish

SpringBoot整合Mybatis【非注解版】-LMLPHPSpringBoot整合Mybatis【非注解版】-LMLPHP


  • 注意:idea必须在联网环境下才会自动构建项目

SpringBoot整合Mybatis【非注解版】-LMLPHPSpringBoot整合Mybatis【非注解版】-LMLPHP


package com.cyn.bean;

/**
* @author:cyn
* @create:2018/12/30 16:14
* @description:员工实体类
*/
public class Employee { private Integer id;
private String lastName;
private Integer gender;
private String email;
private Integer dId; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} public Integer getGender() {
return gender;
} public void setGender(Integer gender) {
this.gender = gender;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public Integer getdId() {
return dId;
} public void setdId(Integer dId) {
this.dId = dId;
} }

SpringBoot整合Mybatis【非注解版】-LMLPHP


  • 新建数据库mybatis,在数据库中新建与实体类对象相关联的数据库表 employee

    DROP TABLE IF EXISTS `employee`;
    CREATE TABLE `employee` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `lastName` varchar(255) DEFAULT NULL,
    `email` varchar(255) DEFAULT NULL,
    `gender` int(2) DEFAULT NULL,
    `d_id` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

    SpringBoot整合Mybatis【非注解版】-LMLPHP


  • 在mapper包下新建与实体类所关联的mapper接口【非注解版】:EmployeeMapper
package com.cyn.mapper;

import com.cyn.bean.Employee;

/**
* @author:崔雅楠
* @create:2018/12/30 17:32
* @description:员工管理
*/
//在这里使用@Mapper注解或在主类中使用@MapperScan注解将该接口扫描装配到容器中
@Mapper
public interface EmployeeMapper { //查询
public Employee getEmpById(Integer id);
//插入
public void insertEmp(Employee employee);
}

SpringBoot整合Mybatis【非注解版】-LMLPHP


  • 在classpath:mybatis/下新建mybatis的主配置文件:mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> <settings>
<!--开启驼峰命名规则-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
  • 在classpath:mybatis/mapper/下新建mapper接口相对应的映射文件例如:EmployeeMapper.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属性进行关联-->
<mapper namespace="com.cyn.mapper.EmployeeMapper">
<!-- public Employee getEmpById(Integer id); public void insertEmp(Employee employee);-->
<select id="getEmpById" resultType="com.cyn.bean.Employee">
SELECT * FROM employee WHERE id=#{id}
</select> <insert id="insertEmp">
INSERT INTO employee(lastName,email,gender,d_id) VALUES (#{lastName},#{email},#{gender},#{dId})
</insert>
</mapper>

  • application.yml(application.properties)项目配置文件中指定mybatis的主配置文件和mapper映射文件的路径,告知springboot,完整application.yml配置文件如下:

    spring:
    datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis
    driver-class-name: com.mysql.jdbc.Driver
    #修改springboot默认数据源为Druid
    type: com.alibaba.druid.pool.DruidDataSourceC3P0Adapter
    #数据源其他配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
    #配置mybatis的主配置文件和mapper映射文件的路径
    mybatis:
    config-location: classpath:mybatis/mybatis-config.xml
    mapper-locations: classpath:mybatis/mapper/*.xml

  • 在controller包下新建controller控制类:EmpController
package com.cyn.controller;

import com.cyn.bean.Employee;
import com.cyn.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; /**
* @author:cyn
* @create:2019/1/1 20:15
* @description:员工管理
*/
@RestController
public class EmpController { @Autowired
EmployeeMapper employeeMapper; //查询
@GetMapping("emp/{id}")
public Employee getEmpById(@PathVariable("id") Integer id){ return employeeMapper.getEmpById(id);
}
//添加
@GetMapping("emp")
public void insertEmp(Employee employee){
employeeMapper.insertEmp(employee);
}
}

附录:


  • 最终项目结构目录如下:

SpringBoot整合Mybatis【非注解版】-LMLPHPSpringBoot整合Mybatis【非注解版】-LMLPHP

二、项目测试

SpringBoot整合Mybatis【非注解版】-LMLPHPSpringBoot整合Mybatis【非注解版】-LMLPHP

SpringBoot整合Mybatis【非注解版】-LMLPHPSpringBoot整合Mybatis【非注解版】-LMLPHP

  • 至此,springboot整合mybatis成功!如果你失败了,继续调试吧骚年!刚搬来博客园,请多多关照手动滑稽--

( ̄︶ ̄)↗[GO!]

05-02 00:28