实体类、DAO层就不赘述了,先添加maven
pom.xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
applicaton.yml
spring:
datasource:
username: root
password: root
#?serverTimezone=UTC解决时区的报错
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
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,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#整合mybatis
mybatis:
type-aliases-package: "com.example.pojo"
mapper-locations: "classpath:mybatis/mapper/*.xml"
servicec层
@Repository
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
private EmployeeMapper employeeMapper;
@Override
public List<Employee> getEmpAll() {
return employeeMapper.getEmpAll();
}
@Override
public Integer addEmp(Employee employee) {
return employeeMapper.addEmp(employee);
}
@Override
public Integer deletEmpById(Integer id) {
return employeeMapper.deleteEmpById(id);
}
@Override
public Integer updateEmpById(Employee employee) {
return employeeMapper.updateEmpById(employee);
}
@Override
public Employee getEmpById(Integer id) {
return employeeMapper.getEmpById(id);
}
}
mapper.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.example.dao.EmployeeMapper">
<resultMap id="empMap" type="com.example.pojo.Employee">
<id property="id" column="id"/>
<result property="lastName" column="lastName"/>
<result property="email" column="email"/>
<result property="gender" column="gender"/>
<result property="birth" column="birth"/>
<association property="department" javaType="com.example.pojo.Department">
<id property="id" column="id"/>
<result property="departmentName" column="dep_name"/>
</association>
</resultMap>
<select id="getEmpAll" resultMap="empMap">
select * from emp e,dep d where e.dep_id=d.id
</select>
<insert id="addEmp" parameterType="Employee">
insert into emp(lastName,email,gender,dep_id,birth) values(#{lastName},#{email},#{gender},#{department.id},#{birth})
</insert>
<delete id="deleteEmpById" parameterType="Integer">
delete from emp where id=#{id}
</delete>
<update id="updateEmpById" parameterType="Employee">
update emp set lastName=#{lastName},email=#{email},gender=#{gender},dep_id=#{department.id},birth=#{birth} where id=#{id}
</update>
<select id="getEmpById" parameterType="Integer" resultMap="empMap">
select * from emp where id=#{id}
</select>
</mapper>
controller层
@Controller
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@Autowired
private DepartmentService departmentService;
@RequestMapping("/emps")
public String empList(Model model){
model.addAttribute("list",employeeService.getEmpAll());
return "emp/list";
}
@GetMapping("/emp")
public String toAdd(Model model){
model.addAttribute("departments",departmentService.getDepAll());
return "emp/add";
}
@PostMapping("/emp")
public String addEmp(Employee employee){
System.out.println("===========>"+employee);
employeeService.addEmp(employee);
return "redirect:/emps";
}
@GetMapping("/emp/{id}")
public String updateEmp(@PathVariable("id")Integer id,Model model){
model.addAttribute("emp",employeeService.getEmpById(id));
model.addAttribute("departments",departmentService.getDepAll());
return "emp/update";
}
@PostMapping("/update")
public String updateEmp(Employee employee){
System.out.println("===========>"+employee);
employeeService.updateEmpById(employee);
return "redirect:/emps";
}
@RequestMapping("/delEmp/{id}")
public String deleteEmp(@PathVariable("id")Integer id){
employeeService.deletEmpById(id);
return "redirect:/emps";
}
}