一、项目创建
- 新建一个工程
- 选择Spring Initializr,配置JDK版本
- 输入项目名
- 选择构建web项目所需的staters(启动器)
- 选择与数据库相关的组件
- 选择保存地址后点击Finish
- 注意:idea必须在联网环境下才会自动构建项目
- 在项目中使用druid作为数据源,不会的请点击SpringBoot整合Druid数据源
- 在bean包下新建实体类对象Employee(员工)
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;
} }
- 新建数据库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;
- 在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);
}
- 在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);
}
}
附录:
- 最终项目结构目录如下:
二、项目测试
- 启动项目,在url地址栏输入插入操作:http://localhost:8080/emp?lastName=AAA&[email protected]&gender=1
- 观察数据库,看是否插入成功
- 再在url地址栏输入查询操作:http://localhost:8080/emp/1
- 至此,springboot整合mybatis成功!如果你失败了,继续调试吧骚年!刚搬来博客园,请多多关照手动滑稽--