Spring框架+jdbcTemplate實(shí)現(xiàn)增刪改查功能
SpringMVC架構(gòu)(Model(實(shí)體類),Service,Controller層)
Controller(接收參數(shù)調(diào)用業(yè)務(wù)層)–>Service(調(diào)用持久層,處理業(yè)務(wù)邏輯)–>Dao(與數(shù)據(jù)庫(kù)交互)
1. IOC(控制反轉(zhuǎn)是一種設(shè)計(jì)思想而不是技術(shù))
DI(依賴注入):是IOC思想的一種技術(shù)實(shí)現(xiàn)
IOC容器是Spring提供的保存Bean對(duì)象的容器
Bean管理操作
1.Xml + 注解
2.javaConfig + 注解
通過(guò)xml配置Bean:TODO:
通過(guò)javaConfig 配置Bean:TODO:
通過(guò)注解配置Bean:TODO:
2. AOP(面向切面)
面向切面的程序設(shè)計(jì)思想。橫向的調(diào)用。
eg:一個(gè)日志的功能,很多的功能模塊都需要去使用,可以寫一個(gè)切面去做這個(gè)事情。
使用@Aspect來(lái)標(biāo)記一個(gè)普通類為切面。
連接點(diǎn):比如說(shuō)日志需要作用的方法。
目標(biāo)對(duì)象:日志需要使用的對(duì)象。
1. 添加依賴
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
<scope>runtime</scope>
</dependency>
2.demo練習(xí)
需求:SpringIOC + JDBCTemplate實(shí)現(xiàn)簡(jiǎn)單的數(shù)據(jù)庫(kù)操作
1.新建Maven項(xiàng)目并引入Spring核心4依賴
<!--Spring的4個(gè)基礎(chǔ)jar包(容器包)-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.3.1</version>
</dependency>
jdbc依賴
<!--Spring整合jdbc-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.6</version>
</dependency>
<!--mysql驅(qū)動(dòng)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
junit5
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
2. 創(chuàng)建SpringConfig配置文件(通過(guò)JavaConfig方式注入bean)
創(chuàng)建SpringConfig類,添加@Configuration標(biāo)記為配置類。
配置數(shù)據(jù)源和JDBCTemplate的Bean。
/**
* @author YonC
* @date 2021/9/2
*/
@Configuration
public class SpringConfig {
@Bean
public DataSource dataSource() {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=ture&charactorEncoding=utf-8&serverTimezone=UTC");
dataSource.setUser("root");
dataSource.setPassword("123456");
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
3.創(chuàng)建MVC架構(gòu)并創(chuàng)建與數(shù)據(jù)庫(kù)字段對(duì)應(yīng)的實(shí)體類對(duì)象
實(shí)體類:StudentPO
public class StudentPO {
private Long id;
private String name;
private String age;
public StudentPO() {
}
public StudentPO(String name, String age) {
this.name = name;
this.age = age;
}
public StudentPO(Long id, String name, String age) {
this.id = id;
this.name = name;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "StudentPO{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
4. 編寫Dao層
面向接口編程,首先定義Dao層的規(guī)范接口,定義了增刪改查4種方法
/**
* @author YonC
* @date 2021/9/2
*/
public interface StudentDao {
void addStudent(StudentPO student);
void delStudentById(Long id);
void updateStudent(StudentPO student);
List<StudentPO> selectStudent();
}
接口的實(shí)現(xiàn)
@Repository注解將StudentDao注入IOC容器
@Autowired自動(dòng)裝配JdbcTemplate對(duì)象,JdbcTemplate對(duì)象已經(jīng)在SpringConfig文件中實(shí)例化
/**
* @author YonC
* @date 2021/9/2
*/
@Repository
public class StudentDaoImpl implements StudentDao {
@Autowired
JdbcTemplate jdbcTemplate;
/*
* 增加Student
* */
@Override
public void addStudent(StudentPO student) {
jdbcTemplate.update("insert into student (name,age) values (?,?)", student.getName(), student.getAge());
}
/*
* 刪除Student
* */
@Override
public void delStudentById(Long id) {
jdbcTemplate.update("delete from student where id=?", id);
}
/*
* 修改Student
* */
@Override
public void updateStudent(StudentPO student) {
String sql = "UPDATE student SET name=?,age=? where id = ? ";
Object[] args = {student.getName(), student.getAge(), student.getId()};
jdbcTemplate.update(sql, args);
}
/*
* 查詢
* */
@Override
public List<StudentPO> selectStudent() {
String sql = "select id,name,age from student";
return this.jdbcTemplate.query(sql, (rs, index) -> {
StudentPO student = new StudentPO();
student.setId(rs.getLong("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
return student;
});
}
}
5. Dao與數(shù)據(jù)庫(kù)的增刪改查已經(jīng)實(shí)現(xiàn),使用Service層去調(diào)用Dao層的方法。
首先定義Service層的接口
/**
* @author YonC
* @date 2021/9/2
*/
public interface StudentService {
void addStudent(StudentPO student);
void delStudentById(Long id);
void updateStudent(StudentPO student);
List<StudentPO> selectStudent();
}
接口實(shí)現(xiàn)
@Service將對(duì)象聲明IOC容器中
@Autowired自動(dòng)裝配IOC容器中的StudentDao將StudentDao對(duì)象初始化
/**
* @author YonC
* @date 2021/9/2
*/
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
StudentDao studentDao;
@Override
public void addStudent(StudentPO student) {
studentDao.addStudent(student);
}
@Override
public void delStudentById(Long id) {
studentDao.delStudentById(id);
}
@Override
public void updateStudent(StudentPO student) {
studentDao.updateStudent(student);
}
@Override
public List<StudentPO> selectStudent() {
return studentDao.selectStudent();
}
}
6. 使用Junit5單元測(cè)試測(cè)試
首先通過(guò)IOC容器拿到StudentService對(duì)象
private AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
// 通過(guò)Spring的IOC容器
private StudentService studentService = applicationContext.getBean(StudentService.class);
測(cè)試
/**
* @author YonC
* @date 2021/9/2
*/
class StudentServiceImplTest {
private AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
// 通過(guò)Spring的IOC容器
private StudentService studentService = applicationContext.getBean(StudentService.class);
@Test
public void testAddStudent() {
studentService.addStudent(new StudentPO("zahngsna", "999"));
System.out.println("添加成功!");
}
@Test
public void testDelStudent() {
studentService.delStudentById(3L);
System.out.println("刪除成功!");
}
@Test
public void testUpdateStudent() {
//將id為3的Student的name修改為"wang",age修改為21
studentService.updateStudent(new StudentPO(1L,"wang","28"));
System.out.println("修改成功!");
}
@Test
public void testSelectStudent() {
studentService.selectStudent().forEach(System.out::println);
}
}
到此這篇關(guān)于Spring框架+jdbcTemplate實(shí)現(xiàn)增刪改查功能的文章就介紹到這了,更多相關(guān)Spring jdbcTemplate增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?如何接收kernel傳過(guò)來(lái)的數(shù)組(推薦)
這篇文章主要介紹了Java?如何接收kernel傳過(guò)來(lái)的數(shù)組,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-08-08
通過(guò)代碼示例了解submit與execute的區(qū)別
這篇文章主要介紹了通過(guò)代碼示例了解submit與execute的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java中的鎖與鎖的狀態(tài)升級(jí)詳細(xì)解讀
這篇文章主要介紹了Java中的鎖與鎖的狀態(tài)升級(jí)詳細(xì)解讀,Java 1.6以后官方針對(duì)鎖的優(yōu)化,主要是增加了兩種新的鎖:偏向鎖和輕量級(jí)鎖,再加上本身重量級(jí)鎖,那么鎖基本上可以大致分為這三種,它們之間的區(qū)別主要是體現(xiàn)在等待時(shí)間上面,需要的朋友可以參考下2024-01-01
Java實(shí)現(xiàn)駝峰與下劃線互轉(zhuǎn)的方法
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)駝峰與下劃線互轉(zhuǎn)的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
SpringBoot使用MyBatis時(shí)的幾種傳參規(guī)范示例
使用Mybatis作為持久層框架時(shí),對(duì)于數(shù)據(jù)庫(kù)的增刪改查等操作都需要參數(shù)的傳遞,本文就詳細(xì)的介紹了一下SpringBoot使用MyBatis時(shí)的幾種傳參規(guī)范示例,感興趣的可以了解一下2022-02-02
SpringBoot集成ip2region實(shí)現(xiàn)ip白名單的代碼示例
ip2region v2.0 - 是一個(gè)離線IP地址定位庫(kù)和IP定位數(shù)據(jù)管理框架,10微秒級(jí)別的查詢效率,提供了眾多主流編程語(yǔ)言的 xdb 數(shù)據(jù)生成和查詢客戶端實(shí)現(xiàn),本文介紹了SpringBoot集成ip2region實(shí)現(xiàn)ip白名單的代碼工程,需要的朋友可以參考下2024-08-08
詳解Java合并數(shù)組的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Java合并數(shù)組的兩種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

