纯注解开发配置spring
生活随笔
收集整理的這篇文章主要介紹了
纯注解开发配置spring
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.純注解開發(fā)【定義配置類的注解】
==@Confituration == 表示該類是一個(gè)配置類
==@ComponentScan(“com.itheima”) == 配置包掃描
@PropertySource(“classpath:jdbc.properties”) 加載屬性文件
==@Import(JdbcConfig.class) == 加載其他配置類
2.spring整合mybatis【純注解,3個(gè)配置類】
<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 //表示該類是一個(gè)配置類,用來(lái)代替applicationContext.xml,這個(gè)注解可以不寫,但是一般都寫了 //<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.注入連接池對(duì)象【必須】ssfb.setDataSource(dataSource);//2.加載MybatisConfig.xml核心配置文件,// 如果核心配置文件中的內(nèi)容都被抽取了,那么就可以不用加載【可選】//ClassPathResource resource = new ClassPathResource("classpath:MybatisConfig.xml");//ssfb.setConfigLocation(resource);//3 配置別名,如果是使用注解配置SQL語(yǔ)句,可以不用配置別名【可選】//ssfb.setTypeAliasesPackage("com.itheima.bean");//4 加載映射配置文件,如果映射配置文件和mapper接口在同一個(gè)包下,并且同名,那么會(huì)自動(dòng)加載【可選】//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.具體實(shí)現(xiàn)類及測(cè)試類
<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的代理對(duì)象@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 {//查詢?nèi)?/span>@Select("select * from student")public abstract List<Student> selectAll();//根據(jù)id查詢@Select("select * from student where id=#{ID}")public abstract Student selectById(Integer id);//新增數(shù)據(jù)@Insert("insert into student values(null,#{name},#{age})")public abstract Integer insert(Student stu);//修改數(shù)據(jù)@Update("update student set name=#{name},age=#{age} where id=#{id}")public abstract Integer update(Student stu);//刪除數(shù)據(jù)@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系統(tǒng)會(huì)默認(rèn)username為系統(tǒng)用戶名 jdbc.username=root jdbc.password=123456??-4>測(cè)試類:
獲取容器方式為加載配置類:
@Testpublic void test1() throws IOException {//從美IOC中取studentService對(duì)象//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));}打印結(jié)果: --------------------------------------------------------------------- Student{id=1, name='張三', age=23} Student{id=2, name='李四', age=24} Student{id=15, name='小郭', age=22}4.spring整合junit單元測(cè)試
前提:添加spring-test和junit依賴,并且junit依賴的版本必須大于等于4.12版本。
【第一步】
<!--junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!--Spring整合junit單元測(cè)試--><!--junit的依賴至少要是4.12版本,可以是4.13等版本,否則出現(xiàn)如下異常--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.1.9.RELEASE</version></dependency>【第二步】
spring的Junit測(cè)試類整合
總結(jié)
以上是生活随笔為你收集整理的纯注解开发配置spring的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 杜甫的三别 杜甫的三别分别是什么
- 下一篇: 自相矛盾告诉我们的道理是什么 自相矛盾故