springboot mybatis mysql 整合
1、pom文件配置
<!--mysql 驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--Test 单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mybatis 引入-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<!--jdbc 引入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
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<Student> findByName(Student student);
public void delete(Student student);
public List<Student> findAll();
}
<?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.zjx.mapper.StudentMapper">
<resultMap type="Student" id="studentMap">
<result column="userName" property="userName"/>
<result column="userAge" property="userAge"/>
<result column="userSex" property="userSex"/>
<result column="userAddress" property="userAddress"/>
</resultMap>
<select id="findAll" resultMap="studentMap" >
select * from student
</select>
<select id="findByName" resultMap="studentMap" parameterType="Student">
select * from student where userName = #{userName}
</select>
<insert id="add" parameterType="Student">
insert into student(userName,userAge,userSex,userAddress)
values
(#{userName},#{userAge},#{userSex},#{userAddress})
</insert>
<delete id="delete" parameterType="String">
delete from student where userName=#{userName}
</delete>
</mapper>
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<Student> 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<Student> 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文件未加载
目录结构:
以上是自学总结。有借鉴前辈帖子望见谅。不足之处请多多指教