Spring通过DataSource获取数据库数据(⾮注解⽅式和注解形式)及Spring
集。。。
⾮注解
这⾥就不多说了,直接来源码
1.导⼊各坐标
这⾥包括数据库连接池(druid/c3p0),单元测试,数据库连接驱动坐标以及spring的依赖就不⼀⼀写出了
jdbctemplate查询一条数据<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.13</version>
</dependency>
2.AccountDaoImpl.java
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate template;
public void setTemplate(JdbcTemplate template) {
}
public void findAll(){
List<Map<String, Object>> list = template.queryForList("select * from teacher");
for (Map<String, Object> map : list) {
for (String s : map.keySet()) {
System.out.println(s+":"+(s));
}
}
}
}
3.AccountServiceImpl.java
public class AccountServiceImpl implements AccountService {
private AccountDaoImpl accountDao;
public void setAccountDao(AccountDaoImpl accountDao) {
this.accountDao = accountDao;
}
@Override
public void findAll() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("l");
AccountDao accountDao = (AccountDao) Bean("accountDao");
accountDao.findAll();
System.out.println("username:"+username);
}
}
l
<bean id="accountDao" class="top.jigw.dao.impl.AccountDaoImpl">
<property name="template" ref="jdbcTemplate"></property>
</bean>
<bean id="accountService" class="top.jigw.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSourceFactory" factory-method="createDataSource">
<constructor-arg name="properties">
<props>
<prop key="driverClassName"&sql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql:///test</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</constructor-arg>
</bean>
<bean id="jdbcTemplate" class="org.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
5.单元测试
public class test {
@Test
public void test01() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("l");
AccountService accountService = (AccountService) Bean("accountService");
accountService.findAll();
}
}
注解形式
1.⾸先同样是导⼊坐标
2.提取jdbc.properties
jdbc.sql.jdbc.Driver
jdbc.url=jdbc:mysql:///test
jdbc.username=root
jdbc.password=123456
3.了解下Spring的⼀些基本注解吧
注解说明
@Component        使⽤在类上⽤于实例化Bean
@Controller            使⽤在web层类上⽤于实例化Bean
@Service    使⽤在service层类上⽤于实例化Bean
@Repository  使⽤在dao层类上⽤于实例化Bean
@Autowired  使⽤在字段上⽤于根据类型依赖注⼊
@Qualifier    结合@Autowired⼀起使⽤⽤于根据名称进⾏依赖注⼊
@Resource  相当于@Autowired+@Qualifier,按照名称进⾏注⼊
@Value    注⼊普通属性
@Scope    标注Bean的作⽤范围
@PostConstruct  使⽤在⽅法上标注该⽅法是Bean的初始化⽅法
@PreDestroy  使⽤在⽅法上标注该⽅法是Bean的销毁⽅法
@Configuration ⽤于指定当前类是⼀个 Spring 配置类,当创建容器时会从该类上加载注解
@ComponentScan ⽤于指定 Spring 在初始化容器时要扫描的包。作⽤和在 Spring 的 xml 配置⽂件中的 <context:component-scan base-package="com.ithei ma"/>⼀样
@Bean 使⽤在⽅法上,标注将该⽅法的返回值存储到 Spring 容器中
@PropertySource ⽤于加载.properties ⽂件中的配置
@Import ⽤于导⼊其他配置类
使⽤注解进⾏开发时,需要在l中配置组件扫描,作⽤是指定哪个包及其⼦包下的Bean需要进⾏扫描以便识别使⽤注解配置的类、字段和⽅法。
<context:component-scan base-package="top.jigw"></context:component-scan>
4.AccountDaoImpl.java
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate template;
public void setTemplate(JdbcTemplate template) {
}
public void findAll(){
List<Map<String, Object>> list = template.queryForList("select * from teacher");
for (Map<String, Object> map : list) {
for (String s : map.keySet()) {
System.out.println(s+":"+(s));
}
}
}
}
5.AccountServiceImpl.java
@Service("accountService")
public class AccountServiceImpl implements AccountService {
private AccountDaoImpl accountDao;
public void setAccountDao(AccountDaoImpl accountDao) {
this.accountDao = accountDao;
}
@Override
public void findAll() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("l");
AccountDao accountDao = (AccountDao) Bean("accountDao");
accountDao.findAll();
System.out.println("username:"+username);
}
}
6.SpringConfiguration.java
@Configuration
@ComponentScan("top.jigw")
@Import({DataSourceConfiguration.class})
public class SpringConfiguration {
}
7.DataSourceConfiguration.java
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {
@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(){
DruidDataSource dataSource = new DruidDataSource();        dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "template")
public JdbcTemplate getTemplate(DataSource dataSource){        return new JdbcTemplate(dataSource);
}
}
集成Junit测试
这⾥需要导⼊spring-test依赖包
Spring集成Junit步骤
①导⼊spring集成Junit的坐标
②使⽤@Runwith注解替换原来的运⾏期
③使⽤@ContextConfiguration指定配置⽂件或配置类
④使⽤@Autowired注⼊需要测试的对象
⑤创建测试⽅法进⾏测试
SpringJunitTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class) public class SpringJunitTest {
@Autowired
private AccountService accountService;
@Test
public void testService(){
accountService.findAll();
}
}
觉得有⽤不如进⼊我的留个⾔吧,谢谢⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。