您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用Spring框架+jdbcTemplate實(shí)現(xiàn)增刪改查功能”,在日常操作中,相信很多人在怎么用Spring框架+jdbcTemplate實(shí)現(xiàn)增刪改查功能問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么用Spring框架+jdbcTemplate實(shí)現(xiàn)增刪改查功能”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
SpringMVC架構(gòu)(Model(實(shí)體類),Service,Controller層)
Controller(接收參數(shù)調(diào)用業(yè)務(wù)層)–>Service(調(diào)用持久層,處理業(yè)務(wù)邏輯)–>Dao(與數(shù)據(jù)庫(kù)交互)
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:
面向切面的程序設(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>
需求: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)增刪改查功能”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。