Spring注解开发Junit、JdbcTemeplate学习
1、Spring注解开发
常用原始注解
- @Controller 在web层类上实例化Bean
- @Service 在service层类上实例化Bean
- @Repository 在dao层上实例化Bean
- @Component 在通用类上实例化Bean
- @Autowired 根据类型依赖注入
- @Qualifier 跟@Autowired一起使用,可根据名称依赖注入
- @Resource 相当于@Autiwired和@Qualifier,根据名称依赖注入
- @Value 注入普通属性
- @Scope 标注Bean的作用范围
- @PostConstruct Bean的初始化方法
- @PreDestroy Bean的销毁方法
常用新注解
- @Configuration 指定Spirng的配置类
- @ComponentScan 指定Spring初始化容器时需要扫描的包
- @Bean 使用在方法上,将返回值存储到Spring容器中
- @PropertySource 用于加载properties文件
- @Import 用于导入其它包
2、案例
使用注解进行开发时,需要在applicationContext.xml中配置组件扫描
<context:component-scan base-package="cn.guixinchn"></context:component-scan>
然后使用注解@Service、@Repository 标识需要Spring实例化的类
使用@Autowired、@Qualifier进行依赖注入
@Repository("userDao")
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("save running....");
}
}
@Service("userServer")
public class UserServerImpl implements UserServer {
@Autowired
@Qualifier("userDao")
private UserDao userDao;
public UserServerImpl(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
userDao.save();
}
}
使用注解充当配置文件
@Configuration
@ComponentScan("cn.guixinchn")
@PropertySource("classpath:jdbc.properties")
public class SpringConfiguration {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean(name="dataSource")
public DataSource getDataSource () throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
}
测试
@Test
public void test4() throws SQLException {
//获取注解配置类的上下文对象
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
//拿到数据源
DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");
//获得连接对象
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
3、Spring集成Junit
3.1、导入spring集成Junit的坐标
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dpendency>
3.2、使用@Runwith注解替换原来的运行期、使用@ContextConfiguration指定配置文件或配置类
3.3、使用@Autowired注入需要测试的对象、使用@Test创建测试方法进行测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class JunitTest {
@Autowired
private UserServer userServer;
@Test
public void test(){
userServer.save();
}
}
4、Spring框架中JdbcTemeplate的使用
4.1导入spring-jdbc和spring-tx坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.3.RELEASE</version>
</depe
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
4.2创建数据库表和实体
public class Student {
private String name;
private int age;
/*省略set个get方法,tostring方法*/
}
4.3让Spirng容器产生JdbcTemplate对象,并获取数据源
<!--Spring容器加载properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
4.4创建测试类执行数据库操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void test1(){
int i = jdbcTemplate.update("insert into student value (?, ?)", "张三", 18);
System.out.println(i);
}
}
4.5测试JdbcTemplate的查询操作
JdbcTemplate模板里面通过RowMapper的实现类BeanPropertyRowMapper,为我们封装实体类
@Test
public void queryAllTest(){
List<Student> studentList = jdbcTemplate.query("select * from student", new BeanPropertyRowMapper<Student>(Student.class));
System.out.println(studentList);
}
@Test
public void queryOneTest(){
Student student = jdbcTemplate.queryForObject("select * from student where name = ?", new BeanPropertyRowMapper<Student>(Student.class), "张三");
System.out.println(student);
}
@Test
public void queryCountTest(){
Long num = jdbcTemplate.queryForObject("select count(*) from student", Long.class);
System.out.println(num);
}