spring mysql整合_springboot mybatis mysql 整合
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文件未加載
目錄結構:
以上是自學總結。有借鑒前輩帖子望見諒。不足之處請多多指教
總結
以上是生活随笔為你收集整理的spring mysql整合_springboot mybatis mysql 整合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java knn分类_返回2个或更多最近
- 下一篇: mysql if 多个_MySQL使用I