步骤:
1.导入需要的jar包
2.创建实体类
3.创建Dao层接口
4.创建resources文件夹 (mybatis-config.xml和applicationContext.xml文件)
5.创建sqlmapper映射文件(匹配dao层接口的方法)
6.获取方法
注:dao层接口实现类 多一属性SqlSessionTemplate sqlsession
Service层接口实现类 多一属性调Dao层方法
applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置数据源 dbcp数据源 1 -->
<!--
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url">
<value><![CDATA[jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8]]></value>
</property>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
-->
<!-- 使用属性文件配置数据源 2-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:db.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url">
<value>${url}</value>
</property>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!-- JNDI数据源 3 -->
<!--Context在Context.xml文件中配置 -->
<!--
<Context>
<Resource name="jdbc/smbms" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8"/>
</Context>
-->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/smbms</value>
</property>
</bean>
<!-- 配置sqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations">
<list>
<!-- 使用**的方式 可以省略一个一个写的繁琐 **会自动扫描dao层包下的xml文件 -->
<value>classpath:cn/a/dao/**/*.xml</value>
</list>
</property>
</bean>
<!--UserMapper 名字与Mapper文件相同 mapperInterface是对应Mapper文件的接口 -->
<bean id="UserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="cn.a.Dao.UserDao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.a.Dao"></property>
</bean>
<!-- -->
<!-- 配置sqlsessionTemplate对象 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<bean id="userDao" class="cn.a.Dao1.Impl.UserDaoImpl" p:sqlsession-ref="sqlSessionTemplate"></bean>
<!-- dao属性可以赋值UserMapper 或 userDap -->
<bean id="userService" class="cn.a.Service1.Impl.UserServiceImpl">
<property name="dao" ref="UserMapper"></property>
</bean>
<!--添加事务-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 新增方法 -->
<tx:method name="Insert" propagation="REQUIRED"/>
<!-- 查询方法 -->
<tx:method name="Select" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
</beans>
自动装配
<bean id="xxx" class="xxx.xx.xx" autowire="byName"/>
no 不适用自动装配
byType 根据属性类型自动装配
byName根据属性名自动装配
constructor 与byType方式类似 不同之处它应用于构造器参数
全局自动装配(最后一行看)
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
default-autowire="byName" >
</beans
拆分Spring配置文件
AppliactionContext ctx=new ClassPathXmlApplicationContext("appliactionContext.xml","appliactionContext2.xml","appliactionContext3.xml");
String[] configs={"appliactionContext.xml","appliactionContext2.xml","appliactionContext3.xml"}
AppliactionContext ctx=new ClassPathXmlApplicationContext(configs)
AppliactionContext ctx=new ClassPathXmlApplicationContext("applicationContext*.xml")
<import resource="applicationContext1.xml"/>
<import resource="applicationContext2.xml"/>
AppliactionContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml")
测试类
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService u=(UserService)ctx.getBean("userService");