(一)、创建项目
在这里插入图片描述
在这里插入图片描述
(二)、基本环境搭建
1)、导入连接池的依赖
<!--引入druid数据源-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
2)、数据源的基本配置
spring:
datasource:
# 数据源基本配置
username: root
password: XHHP0913
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis3?serverTimezone=UTC
type: com.alibaba.druid.pool.DruidDataSource
# 数据源其他配置
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:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
# schema:
# - classpath:sql/department.sql
# - classpath:sql/employee.sql
3)、编写配置类
package springboot06.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* DruidConfig class
*
* @author Flc
* @date 2019/6/19
*/
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DruidDataSource druid(){
return new DruidDataSource();
}
}
4)、编写Model
package springboot06.bean;
public class Employee {
private Integer id;
private String lastName;
private Integer gender;
private String email;
private Integer dId;
public void setId(Integer id) {
this.id = id;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public void setEmail(String email) {
this.email = email;
}
public void setdId(Integer dId) {
this.dId = dId;
}
public Integer getId() {
return id;
}
public String getLastName() {
return lastName;
}
public Integer getGender() {
return gender;
}
public String getEmail() {
return email;
}
public Integer getdId() {
return dId;
}
}
5)、编写Dao层(使用@Mapper或@MapperScan)
package springboot06.dao;
import org.apache.ibatis.annotations.Mapper;
import springboot06.bean.Employee;
@Mapper
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
public void insertEmp(Employee employee);
}
6)、编写Config文件和Mapper文件
<?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>
<?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="springboot06.dao.EmployeeMapper">
<select id="getEmpById" resultType="springboot06.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>
6)、编写其他业务层(这里简化一点,直接使用控制器调用)
package springboot06;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springboot06.bean.Employee;
import springboot06.dao.EmployeeMapper;
/**
* Controller class
*
* @author Flc
* @date 2019/6/20
*/
@Controller
public class TestController {
@Autowired
EmployeeMapper employeeMapper;
@RequestMapping("/emp/{id}")
@ResponseBody
public Employee getEmp(@PathVariable("id") Integer id){
return employeeMapper.getEmpById(id);
}
}
在这里插入图片描述
网友评论