1、pom文件配置
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.2.0
org.springframework.boot
spring-boot-starter-jdbc
2、mybatis 数据库连接配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testpentaho?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations= = classpath:xml/*.xml
mybatis.type-aliases-package=com.zjx.pojo
3.pojo
package com.zjx.pojo;
import org.springframework.context.annotation.Bean;
import java.io.Serializable;
/**
* 学生信息表
*/
public class Student implements Serializable {
private String userName;
private String userAge;
private String userSex;
private String userAddress;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAge() {
return userAge;
}
public void setUserAge(String userAge) {
this.userAge = userAge;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public Student(String userName, String userAge, String userSex, String userAddress) {
this.userName = userName;
this.userAge = userAge;
this.userSex = userSex;
this.userAddress = userAddress;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"userName='" + userName + '\'' +
", userAge='" + userAge + '\'' +
", userSex='" + userSex + '\'' +
", userAddress='" + userAddress + '\'' +
'}';
}
}
4、mapper 接口及映射文件
package com.zjx.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.zjx.pojo.Student;
import java.awt.print.Pageable;
import java.util.*;
@Mapper
public interface StudentMapper {
public void add(Student student);
public List findByName(Student student);
public void delete(Student student);
public List findAll();
}
select * from student
select * from student where userName = #{userName}
insert into student(userName,userAge,userSex,userAddress)
values
(#{userName},#{userAge},#{userSex},#{userAddress})
delete from student where userName=#{userName}
5、service
package com.zjx.service;
import com.zjx.mapper.StudentMapper;
import com.zjx.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
StudentMapper studentMapper;
@CacheEvict(value="student",allEntries = true)
public void add(Student student){
this.studentMapper.add(student);
}
public List findByName(Student student){
return this.studentMapper.findByName(student);
}
@CacheEvict(value="student",allEntries = true)
public void del(Student student){
this.studentMapper.delete(student);
}
@Cacheable(value = "student")
public List allStudent(){
return this.studentMapper.findAll();
}
}
6、测试
package com.zjx;
import com.zjx.pojo.Student;
import com.zjx.service.StudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootehcacheApplicationTests {
@Autowired
StudentService studentService;
@Test
public void contextLoads() {
}
@Test
public void getStudent(){
//第一次
System.out.println(this.studentService.allStudent().size());
this.studentService.add(new Student("aa","24","男","华阳大道"));
this.studentService.del(new Student("cc","","",""));
this.studentService.del(new Student("bb","","",""));
this.studentService.del(new Student("aa","","",""));
//第二次
System.out.println(this.studentService.allStudent().size());
}
}
运行出现 invalid bound statement (not found)异常
原因是mapper.xml文件未加载
目录结构:
以上是自学总结。有借鉴前辈帖子望见谅。不足之处请多多指教