< ? xml version= "1.0" encoding= "UTF-8" ? >
< beans xmlns= "http://www.springframework.org/schema/beans"
xmlns: xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= "http: / / www. springframework. org/ schema/ beans
http: / / www. springframework. org/ schema/ beans/ spring- beans. xsd">
< ! -- 通过加载dbcp. properties-- >
< bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
< property name= "location" >
< value> classpath: dbcp. properties< / value>
< / property>
< / bean>
< ! -- 配置数据库信息-- >
< ! -- destroy- method= "close"
将BasicDataSource这个类中的destroy方法设置为关闭,即不销毁;
所以可以理解为
当数据库连接不使用的时候, 就把该连接重新放到数据池中, 方便下次使用调用-- >
< bean id= "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy- method= "close" >
< property name= "driverClassName" value= "${driverClassName}" > < / property>
< property name= "url" value= "${url}" > < / property>
< property name= "username" value= "${username}" > < / property>
< property name= "password" value= "${password}" / >
< / bean>
< ! -- sqlSessionFactory的属性包含数据源,mybatis配置文件以及mybatis的mapper文件-- >
< ! -- 在mybatis的配置文件为空的时候,可以和我一样,把他注释掉-- >
< bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" >
< property name= "dataSource" ref= "dataSource" / >
< ! -- < property name= "configLocation" value= "classpath:conf.xml" / > -- >
< property name= "mapperLocations" value= "classpath:mapper/*.xml" / >
< / bean>
< ! --
第一中方式. 把spring配置的SQLSessionFactory交给Dao ,需要写实现类PersonImpl
-- >
< ! -- < bean id= "personMapper" class = "mapper.PersonImpl" >
< property name= "sqlSessionFactory" ref= "sqlSessionFactory" / >
< / bean> -- >
< bean id= "personService" class = "Service.PersonServiceImpl" >
< property name= "personMapper" ref= "personMapper" / >
< / bean>
< ! -- 第二种方式, MapperFactoryBean不用写实现类, 基于接口开发, 这个时候mapper. PersonImpl就不用了
缺点;每个mapper都需要配一次
-- >
< ! -- < bean id= "personMapper" class = "org.mybatis.spring.mapper.MapperFactoryBean" >
< property name= "mapperInterface" value= "Dao.PersonMapper" > < / property>
< property name= "sqlSessionFactory" ref= "sqlSessionFactory" > < / property>
< / bean>
-- >
< ! -- 第三种方式. 简化mapper的数量,只需要配一次mapper就行了,批量配置-- >
< ! -- mapperScannerConfigurer, 他的属性可以扫描包下的所有接口mapper-- >
< ! -- sqlSessionFactoryBeanName, 如果配置了多个sqlSessionFactory,则需要把指定的bean名写入value, 而不是ref-- >
< ! -- 此时,Service注入的属性不是mappers, 而是他包类下的mapper具体值,所以别的地方不变-- >
< bean id= "mappers" class = "org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name= "basePackage" value= "Dao" > < / property>
< property name= "sqlSessionFactoryBeanName" value= "sqlSessionFactory" > < / property>
< / bean>
< / beans>
mybatis的conf.xml因为内容都加到了spring的conf.xml中,就不再赘述 下面是一个简单的mapper配置
< ?xml version= "1.0" encoding= "UTF-8" ?>
< ! DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< ! -- namespace:该mapper.xml映射文件的 唯一标识 -->
< mapper namespace= "mapper.PersonMapper" >
< ! -- < select id= "queryPersonById" parameterType= "int" resultType= "entity.Person" >
select * from person where id =
< /select>
-->
< insert id= "addPerson" parameterType= "entity.Person" >
insert into person values(
< /insert>
< /mapper>
package mapper;
import entity. Person;
import org. apache. ibatis. session. SqlSession;
import org. mybatis. spring. support. SqlSessionDaoSupport;
public class PersonImpl extends SqlSessionDaoSupport implements PersonMapper {
public void addPerson ( Person person) {
SqlSession sqlSession = super . getSqlSession ( ) ;
PersonMapper personMapper = sqlSession. getMapper ( PersonMapper. class ) ;
personMapper. addPerson ( person) ;
}
}
这样配置,就可以实现了spring-mybatis来进行MySQL操作 欢迎提问,或者指正