非注解
这里就不多说了,直接来源码
1.导入各坐标
这里包括数据库连接池(druid/c3p0),单元测试,数据库连接驱动坐标以及spring的依赖就不一一写出了
<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) {
this.template = 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+":"+map.get(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("applicationContext.xml");
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
accountDao.findAll();
System.out.println("username:"+username);
}
}
4.applicationContext.xml
<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">com.mysql.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.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
5.单元测试
public class test {
@Test
public void test01() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) applicationContext.getBean("accountService");
accountService.findAll();
}
}
注解形式
1.首先同样是导入坐标
2.提取jdbc.properties
jdbc.driver=com.mysql.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.itheima"/>一样
@Bean 使用在方法上,标注将该方法的返回值存储到 Spring 容器中
@PropertySource 用于加载.properties 文件中的配置
@Import 用于导入其他配置类
使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的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) {
this.template = 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+":"+map.get(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("applicationContext.xml");
AccountDao accountDao = (AccountDao) applicationContext.getBean("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();
}
}
觉得有用不如进入我的Github博客留个言吧,谢谢支持!