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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

纯注解开发配置spring

發布時間:2023/12/3 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 纯注解开发配置spring 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.純注解開發【定義配置類的注解】

==@Confituration == 表示該類是一個配置類
==@ComponentScan(“com.itheima”) == 配置包掃描
@PropertySource(“classpath:jdbc.properties”) 加載屬性文件
==@Import(JdbcConfig.class) == 加載其他配置類

2.spring整合mybatis【純注解,3個配置類】

<1>SpringConfig配置類

import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource;@Configuration //表示該類是一個配置類,用來代替applicationContext.xml,這個注解可以不寫,但是一般都寫了 //<context:component-scan base-package="com.itheima"/> @ComponentScan("com.itheima") //Spring包掃描 //<context:property-placeholder location="classpath*:*.properties"/> @PropertySource("classpath:jdbc.properties") //加載屬性文件 // <import resource="classpath:applicationContext-dao.xml"/> @Import(JdbcConfig.class) //引入分配置類 public class SpringConfig { }

<2>JdbcConfig配置類

import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean;import javax.sql.DataSource;public class JdbcConfig {@Value("${jdbc.driver}")private String driverClassName;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;@Beanpublic DataSource dataSource() {DruidDataSource ds = new DruidDataSource();ds.setDriverClassName(driverClassName);ds.setUrl(url);ds.setUsername(username);ds.setPassword(password);return ds;} }

<3>MybatisConfig 配置

import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import javax.sql.DataSource;public class MybatisConfig {@Beanpublic SqlSessionFactoryBean SqlSessionFactoryBean(@Autowired DataSource dataSource){SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();//1.注入連接池對象【必須】ssfb.setDataSource(dataSource);//2.加載MybatisConfig.xml核心配置文件,// 如果核心配置文件中的內容都被抽取了,那么就可以不用加載【可選】//ClassPathResource resource = new ClassPathResource("classpath:MybatisConfig.xml");//ssfb.setConfigLocation(resource);//3 配置別名,如果是使用注解配置SQL語句,可以不用配置別名【可選】//ssfb.setTypeAliasesPackage("com.itheima.bean");//4 加載映射配置文件,如果映射配置文件和mapper接口在同一個包下,并且同名,那么會自動加載【可選】//ClassPathResource resource = new ClassPathResource("classpath:StudentMapper.xml");//ssfb.setMapperLocations(resource);return ssfb;}@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer msc = new MapperScannerConfigurer();msc.setBasePackage("com.itheima.mapper");return msc;} }

3.具體實現類及測試類

<1>StudentServiceImpl類:

package com.itheima.service.impl;import com.itheima.bean.Student; import com.itheima.mapper.StudentMapper; import com.itheima.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.io.IOException; import java.util.List;@Service("studentService") public class StudentServiceImpl implements StudentService {@Autowiredprivate StudentMapper mapper;//注入StudentMapper的代理對象@Overridepublic List<Student> selectAll() throws IOException {return mapper.selectAll();}@Overridepublic Student selectById(Integer id) throws IOException {return mapper.selectById(id);}@Overridepublic Integer insert(Student stu) throws IOException {return mapper.insert(stu);}@Overridepublic Integer update(Student stu) throws IOException {return mapper.update(stu);}@Overridepublic Integer delete(Integer id) throws IOException {return mapper.delete(id);} }

<2>studentMapper類

package com.itheima.mapper; import com.itheima.bean.Student; import org.apache.ibatis.annotations.*;import java.util.List; public interface StudentMapper {//查詢全部@Select("select * from student")public abstract List<Student> selectAll();//根據id查詢@Select("select * from student where id=#{ID}")public abstract Student selectById(Integer id);//新增數據@Insert("insert into student values(null,#{name},#{age})")public abstract Integer insert(Student stu);//修改數據@Update("update student set name=#{name},age=#{age} where id=#{id}")public abstract Integer update(Student stu);//刪除數據@Delete(" delete from student where id=#{id}")public abstract Integer delete(Integer id); }

<3>jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis #不加Jdbc系統會默認username為系統用戶名 jdbc.username=root jdbc.password=123456

??-4>測試類:

獲取容器方式為加載配置類:

@Testpublic void test1() throws IOException {//從美IOC中取studentService對象//ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);StudentService studentService = applicationContext.getBean("studentService", StudentService.class);List<Student> list = studentService.selectAll();list.forEach(stu-> System.out.println(stu));}打印結果: --------------------------------------------------------------------- Student{id=1, name='張三', age=23} Student{id=2, name='李四', age=24} Student{id=15, name='小郭', age=22}

4.spring整合junit單元測試

前提:添加spring-test和junit依賴,并且junit依賴的版本必須大于等于4.12版本。

【第一步】

<!--junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!--Spring整合junit單元測試--><!--junit的依賴至少要是4.12版本,可以是4.13等版本,否則出現如下異常--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.1.9.RELEASE</version></dependency>

【第二步】
spring的Junit測試類整合

import com.itheima.bean.Student; import com.itheima.config.SpringConfig; import com.itheima.service.StudentService; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.io.IOException; import java.util.List;//【第二步】使用spring中提供的單元測試運行類替換JVM中單元測試運行類 @RunWith(SpringJUnit4ClassRunner.class) //【第三步】加載配置文件或者配置類[將bean對象注入容器] @ContextConfiguration(classes = {SpringConfig.class}) public class StudentJunit {@Autowired//[將studentService采用set注入方式,則不能new容器對象獲取]private StudentService studentService;@Testpublic void test1() throws IOException {List<Student> list = studentService.selectAll();//斷言測試,與預期一致則通過,反正爆出不同的地方Assert.assertEquals(24,list.size());}@Testpublic void test2() throws IOException {Student stu = studentService.selectById(20);Assert.assertEquals("陳錦濤",stu.getName());} }

總結

以上是生活随笔為你收集整理的纯注解开发配置spring的全部內容,希望文章能夠幫你解決所遇到的問題。

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