日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

spring mysql整合_springboot mybatis mysql 整合

發布時間:2023/12/10 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 整合的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。