1.PropertySource注解
读取配置,注解当前类,参数为对应的配置文件路径,使用这种方式加载配置文件,可以不用在xml中配置PropertiesFactoryBean引入jdbc.properties
作用:用来指定properties文件的位置
属性:
value:指定文件的名称和路径
关键字:classpath,表示类路径
属性文件
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/cms
jdbc.username=root
jdbc.password=root
package edu.config;
import com.alibaba.druid.pool.DruidDataSource;
import edu.xalead.ChannelService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import javax.sql.DataSource;
import java.sql.Connection;
/**
* 定义配置类,用来替换xml文件,被注解的类包含有一个或多个被@Bean注解的方法,
* 这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,
* 并用于构建bean定义,初始化Spring容器。
*/
@Configuration
@PropertySource("classpath:jdbcConfig.properties")
public class OtherConfig {
private Connection conn = null;
@Value("${jdbc.driverClass}")
private String driverClass;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean("channelService")
public ChannelService createChannelService(DataSource dataSource){
ChannelService cs = new ChannelService();
cs.setDataSource(dataSource);
return cs;
}
@Bean("dataSource")//把DataSource()方法返回的对象放入bean工厂
@Scope("prototype")//匹配多例模式
public DruidDataSource druidDataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setUrl(url);
ds.setDriverClassName(driverClass);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
2.Spring整合unit的配置
添加spring test库的坐标
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
<scope>test</scope>
</dependency>
@RunWith(SpringJUnit4ClassRunner.class)注解
RunWith是一个运行器 SpringJUnit4ClassRunner.class指让测试运行于Spring的运行环境内
@ContextConfiguration注解是引入配置文件
import edu.config.SpringConfig;
import edu.xalead.ChannelService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import javax.sql.DataSource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class Test1 {
// private BeanFactory factory;
/**
* 之前是从工厂里面拿DataSource
* 而现在加了注解配置之后,就不用在从工厂里面取了
* 直接利用@Resource注解从Spring工厂里面取
*/
@Resource
private DataSource ds = null;
// @Before
// public void init(){
// factory = new AnnotationConfigApplicationContext(SpringConfig.class);
// }
@Resource
private ChannelService cs = null;
@Test
public void test1(){
System.out.println(" ");
}
@Test
public void test2(){
System.out.println();
}
}
3.Spring中的JdbcTemplate
JdbcTemplate是Spring框架中的一个对象,是对原始Jdbc API对象的简单封装,为我们提供操作模板类。
环境搭建
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
使用JdbcTemplate对数据库进行操作
import edu.config.SpringConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class Test2 {
@Resource
private JdbcTemplate jdbcTemplate;
/**
* 添加测试
*/
@Test
public void test1() {
String sql = "insert into t_channel(cid,cname,description) values(288,'音乐剧','音乐剧')";
jdbcTemplate.execute(sql);
}
/**
* 删除测试
*/
@Test
public void test2() {
String sql = "delete from t_channel where cname = '教育'";
jdbcTemplate.execute(sql);
}
/**
* 更新测试
*/
@Test
public void test3() {
String sql = "update t_channel set cname = ?,description = ? where cid = ?";
jdbcTemplate.update(sql,"教育","教育",12);
}
/**
* 查询列表测试
*/
@Test
public void test4() {
String sql = "select cid,cname,description from t_channel";
List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
System.out.println(list);
for(Map<String,Object> row : list){
System.out.println(row.get("cid"));
System.out.println(row.get("cname"));
System.out.println(row.get("description"));
}
}
/**
* 查询一条记录的测试
*/
@Test
public void test5() {
String sql = "select cid,cname,description from t_channel where cid = ?";
Channel c = jdbcTemplate.queryForObject(sql, new RowMapper<Channel>() {
@Override
public Channel mapRow(ResultSet rs, int i) throws SQLException {
Channel c = new Channel();
c.setCid(rs.getInt("cid"));
c.setCname(rs.getString("cname"));
c.setDescription(rs.getString("description"));
return c;
}
},288);
System.out.println(c);
}
/**
* 查询一条基本类型(Integer,String等)数据的测试
*/
@Test
public void test6() {
String sql = "select count(*) from t_channel";
Integer a = jdbcTemplate.queryForObject(sql,Integer.class);
System.out.println(a);
}
}