所需要的jar包
配置文件:
一:
把mybatis做的連接數據庫的工作交給spring做了
mybatis做的工作就是管理日誌文件
接下來就是beans,加載資源文件,使用spring自帶的佔位符替換功能
給spring-beansjar包中org.springframework.beans.factory.config包中的PropertyPlaceholderConfigurer注入值,之後有更多的配置文件也可以寫到list標籤中,方便多個配置文件加載,連接了數據庫
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 允許JVM參數覆蓋 -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<!-- 忽略沒有找到的資源文件 -->
<property name="ignoreResourceNotFound" value="true" />
<!-- 配置資源文件 -->
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
二:
連接數據源 (四種方法):一定要注意配置屬性時候的值 propert 的name每種連接方式是不一樣的
1 可以用自帶方法配置 給spring-jdbc包中org.springframework.jdbc.datasource的DriverManagerDataSource類注入值
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
2 可以使用c3p0連接池 c3p0jar包中com.mchange.v2.c3p0中的DriverManagerDataSource<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<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>
3 dbcp commons-dbcp2-2.1.1.jar中的org.apache.commons.dbcp2.BasicDataSource<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
4 阿里巴巴數據源(現在比較火) 給druid-1.0.20.jar中的com.alibaba.druid.pool中的DruidDataSource注入值
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
三:
配置spring和mybatis的連接,用bean配置的方式生成sqlsessionfactoty 在mybatis-spring連接的jar包中,省去了寫單獨用mybatis時候寫的得到sqlsession的方法
<!-- spring和mybatis溝通 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- <property name="mapperLocations" value="classpath:lu/nynu/mapper/*Mapper.xml"> </property> -->
</bean>
四:
配置mapper映射,方法也在mybatis-springjar包中
<!-- mapper代理方式,批量代理Mapper接口代理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
<property name="basePackage" value="lu.nynu.mapper"></property>
</bean>
到此dao(數據層)需要的配置就全部配好了
直接調用dao層的是service層service層處理的是業務邏輯
spring中的bean用於注入屬性值,可以用context中的方法根據類中註解自動掃描 包中的類和屬性
<context:component-scan base-package="lu.nynu.service"></context:component-scan> <!-- 掃描類和屬性 -->
類中的註解重要的五種:
1 @component: 可以使用註解掃描spring中的bean,它是一個泛化概念,可以表示任何層,如果這個類不屬於數據層(dao),業務邏輯層(service),控制層(controller),就可以用此註解
2 @repository: 用於數據訪問(dao層)
3 @service: 用於service層
4 @controller: 用於controller層
5 @resource: 按照bean的實例名裝配,相當於配置property ,按照name(屬性名),type(屬性類型)裝配
使用方法:
package lu.nynu.service;
import javax.annotation.Resource;
import lu.nynu.mapper.CustomerMapper;
import lu.nynu.po.Customer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service//類上標註是那一層
public class CustomerService {
@Resource//需要實例化的對象
CustomerMapper customerMapper;
public Customer selectCustomerById(Integer id){
return customerMapper.selectCustomerById(id);
}
public Integer insertCustomer(Customer customer){
int i=customerMapper.insertCustomer(customer);
int j=3/0;
return i;
}
}
因爲我的xml文件是分開寫的,如果要導入內的配置文件
<import resource="classpath:applicationContext-*.xml"/>
這樣直接走text方法就得到值了
package lu.nynu.test;
import javax.annotation.Resource;
import lu.nynu.po.Customer;
import lu.nynu.service.CustomerService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)//junit測試方法,需要導入junit4jar包
@ContextConfiguration("classpath:beans.xml")
// 加載spring配置文件
public class Test1 {
@Resource
CustomerService customerService;
@Test
public void test1() {
System.out.println(customerService.selectCustomerById(1));
}
@Test
public void test2() {
Customer customer=new Customer();
customer.setUsername("張123");
customer.setJobs("student");
customer.setPhone("12345");
customerService.insertCustomer(customer);
}
}