非註解
這裏就不多說了,直接來源碼
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博客留個言吧,謝謝支持!