MyBatis-Spring连接
Mybatis-Spring
1.准备JAR包
略
2.编写配置文件
在类路径下建立下面几个文件
# db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
<!--applicationContext.xml-->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<!--数据库驱动-->
<property name="driverClassName" value="${jdbc.driver}"/>
<!--连接数据库的 url -->
<property name="url" value="${jdbc.url}"/>
<!--连接数据库的用户名-->
<property name="username" value="${jdbc.username}"/>
<!--连接数据库的密码-->
<property name="password" value="${jdbc.password}"/>
<!--最大连接数-->
<property name="maxTotal" value="${jdbc.maxTotal}"/>
<!--最大空闲连接-->
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<!--初始化连接数-->
<property name="initialSize" value="${jdbc.initialSize}"/>
</bean>
<!--Spring 事务管理(需要配置具体管理的数据源)-->
<bean id="transactionManager" class=
"org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--事务开启注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--配置MyBatis工厂-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--mybatis-config.xml-->
<configuration>
<typeAliases>
<!--配置别名-->
<package name="com.itheima.po"/>
</typeAliases>
<mappers>
</mappers>
</configuration>
3.Mapper接口整合
1.编写Mapper文件和Mapper接口
<!--CustomerMapper.xml-->
<mapper namespace="com.itheima.mapper.CustomerMapper">
<select id="findCustomerById" parameterType="Integer"
resultType="Customer">
select * from t_customer where id=#{id}
</select>
</mapper>
// CustomerMapper.java
public interface CustomerMapper {
public Customer findCustomerById(Integer id);
}
2.主配置文件中添加映射文件
<mappers>
<mapper resource="com/itheima/mapper/CustomerMapper.xml"/>
</mappers>
3.将接口包装为MapperFactoryBean
<bean id="customerMapper"-->
class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface"
value="com.itheima.mapper.CustomerMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
MapperFactoryBean会根据传过来的接口,生成对应的类
4.测试
@Test
public void findCustomerByIdMapperTest(){
ApplicationContext act =
new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerMapper customerMapper = act.getBean(CustomerMapper.class);
Customer customer = customerMapper.findCustomerById(1);
System.out.println(customer);
}
5.实际开发
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.mapper"/>
</bean>
Spring中加入后不需要 2,3步骤配置即可自动扫描完成
4.事务测试
<!--applicationContext.xml中加入注解扫描-->
<context:component-scan base-package="com.itheima.service"/>
编写事务接口的实现类
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService{
@Autowired
private CustomerMapper customerMapper;
@Override
public void addCustomer(Customer customer) {
this.customerMapper.addCustomer(customer);
int i=1/0; //制造异常
}
}
//测试
@Test
public void findCustomerByIdDaoTest(){
ApplicationContext act =
new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerDao customerDao;
//customerDao = (CustomerDao) act.getBean("customerDao");
customerDao = act.getBean(CustomerDao.class);
Customer customer = customerDao.findCustomerById(1);
System.out.println(customer);
}