一、Spring配置数据源
在Spring中也是支持JdbcTemplate,用来操作数据库
相关坐标:
<dependencies><!--mysql驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><!--druid连接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.15</version></dependency><!--spring-jdbc--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.1.5.RELEASE</version></dependency><!--spring的核心坐标--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.5.RELEASE</version></dependency><!--junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies><build><plugins><!-- 设置编译版本为1.8 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build>
jdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/数据库名?characterEncoding=UTF-8jdbc.username=rootjdbc.password=1234
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"<!--在Spring中加载properties引入相关约束-->xmlns:context="http://www.springframework.org/schema/context"http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 通过此标签将文件加载到内存中--><context:property-placeholderlocation="classpath:jdbc.properties"></context:property-placeholder></beans>
Druid&JdbcTemplate:
<!--配置Druid数据库连接池--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driverClassName}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!--配置JdbcTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean>
测试:
@Testpublic void test01() {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);String sql = "select * from student";List<Student> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));System.out.println(list);}
二、Spring原注解
Spring是轻代码而重配置的框架,框架比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率
==Spring5.0以前提供的注解成为原始注解==
1、开启注解
想要使用注解开发,必须在Spring核心配置文件中开启注解才能使用
<!--开启注解扫描--><context:component-scan base-package="com.smiledog"/>
2、IoC注解
@Component
@Controller
@Service
使用在service层类上实例化Bean(标记在Service层实现类的上方)
@Repository
使用在dao层上用于实例化Bean,(标记在dao层实现类的上方)
3、DI注解
@Autowired
使用字段上用于根据类型依赖注入,相当于
标记在service实现类中创建的dao层字段上或set方法上
注:若有多个dao层实现类会出错—>找到多个实现类
@Qualifier
结合@Autowired一起使用,然后根据id查找同类型下的实例
结合使用时在@Qualifier(”标记dao层实现类名”),标记位置同上
@Resource
相当于@Autowired+@Qualifier,按照名称进行注入
用法:@Resource(name=”标记dao层实现类名”),标记位置同上
4、其他注解
@Scope
标注Bean的作用范围(singleton:单例对象【默认】prototype:多例对象)
标记在实现类的上方:@Scope(”singleton / prototype”)
@PostConstruct
使用在方法上标注该方法是Bean的初始化(init())方法
定义一个初始化要执行的方法,该注解标记其上方
@PreDestroy
使用在方法上标注该方法是Bean的销毁方法
定义一个销毁时需要执行的方法,该注解标记其上方
@Value
注入普通属性,主要用于读取配置文件中的值,并赋值给当前属性
标记在一个实体类的私有字段上方:@Value(值)
5、在配置文件中获取值
1、在Spring主配置文件中,加载properties
<context:property-placeholder location="classpath:jdbc.properties" />
2、使用@Value获取配置文件中的值,赋值给属性
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/spring_test1?characterEncoding=UTF-8jdbc.username=rootjdbc.password=root
@Value("${jdbc.driverClassName}")private String driverClass;@Value("${jdbc.url}")private String url;@Value("${jdbc.password}")private String password;
三、Spring注解整合JDBC
1、applicationContext.xml的编写
a、头约束
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"></beans>
b、开启注解扫描
<!--开启注解扫描--><context:component-scan base-package="com.smiledog"/>
c、引入jdbc.properties配置文件
<!--引入jdbc.properties配置文件--><context:property-placeholder location="classpath:jdbc.properties"/>
d、配置Druid数据库连接池
<!--配置Druid数据库连接池--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driverClassName}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean>
e、配置jdbcTemplate
<!--配置jdbcTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean>
四、Spring新注解
@Configuration
表示当前类是一个配置类,用于代替配置文件(applicationContext.xml)
@Bean
@PropertySource
@ComponentScan
用于指定扫描包路径(Value:用于指定路径,数组 basePackages:同Value)
==>:
@Import
用于合并配置类,相当于(
标记在主配置类上方:@Import(DataSourceConfig.class)
1、编写配置类
1、在类的上方标记Configuration表示该类是个配置类@Configuration2、标记ComponentScan用于指定该配置类扫描的包路径@ComponentScan("com.smiledog")3、标记PropertySource用于引入第三方配置文件@PropertySource("classpath:jdbc.properties")4、标记@Value用于获取第三方配置i文件的内容@Value("${jdbc.driverClassName}")标记在字段上方5、标记@Bean在获取连接池的方法上@Bean("dataSoruce")6、标记@Autowired在获取JdbcTemplate方法上@Bean @Autowired
/*** @Configuration 相当于 applicationContext.xml配置文件*/@Configuration/*** @ComponentScan 相当于* <context:component-scan base-package="com.itfxp"/>*/@ComponentScan("com.smiledog")/*** @PropertySource 相当于* <context:property-placeholder location="classpath:jdbc.properties"/>*/@PropertySource("classpath:jdbc.properties")public class SpringApplicationContextConfig {@Value("${jdbc.driverClassName}")private String driverClassName;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;/*自定义DataSource对象交给IoC容器<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driverClassName}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean>*/@Bean("dataSource")public DataSource createDataSource() {DruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setDriverClassName(driverClassName);druidDataSource.setUrl(url);druidDataSource.setUsername(username);druidDataSource.setPassword(password);return druidDataSource;}/**自定义JdbcTemplate对象交给IoC容器<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean>*/@Bean@Autowiredpublic JdbcTemplate createJdbcTemplate(DataSource dataSource){return new JdbcTemplate(dataSource);}}
1、模块化配置类
主配置类中引入其他配置类:/*** @Import 相当于* <import resource=""></import>*/在主配置类的上方标记:@Import(DataSourceConfig.class)
五、Spring注解整合Junit
1、在测试类的上方加入注解加载单元测试运行器
@RunWith(SpringRunner.class)
2、在测试类的上方加入注解加载Spring核心配置
@ContextConfiguration("classpath:applicationContext.xml") //加载配置文件@ContextConfiguration(classes = SpringApplicationContextConfig.class) //加载配置类
