Spring + myBatis 配置文件演进过程

一、配置单个Dao实现:

mapper/productMapper.xml:

<?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 namespace="org.mybatis.example.BlogMapper">

<insert id="ss" parameterType="com.day11.ssm.spring.mybatis.model.Product" >
INSERT INTO product (product_name, sale_price) VALUES (#{productName},#{salePrice})
</insert>

</mapper>


spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 读取配置文件 -->
<context:property-placeholder location="classpath:db.properties" />

<!-- 配置datasources -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/station_demo"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>

<!-- 整合mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 属性-->
<!-- 映射文件地址 -->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
</bean>

<!-- ProduuctDao -->
<bean id="productDao" class="com.day11.ssm.spring.mybatis.dao.impl.ProductDao">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

</beans>
二、配置单个Dao接口(不需要写具体的实现类):

mapper/productMapper.xml:

<?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 namespace="com.day11.ssm.spring.mybatis.dao.IProductDao">

<insert id="save" parameterType="com.day11.ssm.spring.mybatis.model.Product" >
INSERT INTO product (product_name, sale_price) VALUES (#{productName},#{salePrice})
</insert>

</mapper>

spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 读取配置文件 -->
<context:property-placeholder location="classpath:db.properties" />

<!-- 配置datasources -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/station_demo"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>

<!-- 整合mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 属性-->
<!-- 映射文件地址 -->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
</bean>

<!-- ProduuctDao -->
<!-- <bean id="productDao" class="com.day11.ssm.spring.mybatis.dao.impl.ProductDao">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean> -->

<!-- ProduuctDao -->
<bean id="mapperFactoryBean" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="mapperInterface" value="com.day11.ssm.spring.mybatis.dao.IProductDao"/>
</bean>

</beans>

三、配置扫描包(多个Dao接口),此为生产上的终极配置方式

mapper/productMapper.xml:

<?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 namespace="com.day11.ssm.spring.mybatis.dao.IProductDao">

<insert id="save" parameterType="com.day11.ssm.spring.mybatis.model.Product" >
INSERT INTO product (product_name, sale_price) VALUES (#{productName},#{salePrice})
</insert>

</mapper>

spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 读取配置文件 -->
<context:property-placeholder location="classpath:db.properties" />

<!-- 配置datasources -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/station_demo"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>

<!-- 整合mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 属性-->
<!-- 映射文件地址 -->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
</bean>

<!-- ProduuctDao -->
<!-- <bean id="productDao" class="com.day11.ssm.spring.mybatis.dao.impl.ProductDao">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean> -->

<!-- ProduuctDao -->
<!-- <bean id="mapperFactoryBean" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="mapperInterface" value="com.day11.ssm.spring.mybatis.dao.IProductDao"/>
</bean> -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.day11.ssm.spring.mybatis.dao"/>
</bean>

</beans>





posted @ 2020-10-28 10:29  一天从晚上开始  阅读(94)  评论(0编辑  收藏  举报