一.解析图
二.范例
1.业务层【service】
(1)代码块
接口类
package cn.service;
import cn.domain.Account;
import java.util.List;
public interface AccountService {
//查询所有账户信息
public List<Account> findAll();
//保存账户信息
public void saveAccount(Account account);
}
实现类
package cn.service.impl;
import cn.dao.AccountDao;
import cn.service.AccountService;
import cn.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
/**
* 查询所有用户
* @return
*/
@Override
public List<Account> findAll() {
System.out.println("查询业务层代码块【查询所有账户】。。。。。");
return accountDao.findAll();
}
/**
* 添加用户
* @param account
*/
@Override
public void saveAccount(Account account) {
System.out.println("查询业务层代码块【添加所有账户】。。。。。");
// int num = 2/0; //测试事务是否回滚
accountDao.saveAccount(account);
}
}
(2)范例一【接口类】
(3)范例二【实现类】
2.web.xml
(1)代码块
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置spring的监听器的加载位置信息的参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载springMVC的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!-- 启动服务器,加载该servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 中文解析器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
(2)范例
3.spring的配置文件
(1)代码块
<?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:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解的扫描,希望处理service、dao层,controller不需要处理 -->
<context:component-scan base-package="cn.service">
<!-- 配置哪些注解不需要扫描,type指的是数据类型,expression是类的全限定类名 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<!-- 配置哪些注解需要扫描,type指的是数据类型,expression是类的全限定类名 -->
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>-->
</context:component-scan>
<!-- spring整合mybatis框架 -->
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置sqlSessionFactory工厂对象 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入连接池对象 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置接口所在的包 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置接口所在的包 -->
<property name="basePackage" value="cn.dao"></property>
</bean>
<!-- 配置spring框架声明式事务管理 -->
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 配置事务管理器 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 如果是查询方法,则设置为只读权限 -->
<tx:method name="find*" read-only="true"/>
<tx:method name="*" isolation="DEFAULT"></tx:method>
</tx:attributes>
</tx:advice>
<!-- 配置增强AOP -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.service.impl.*ServiceImpl.*(..))"></aop:advisor>
</aop:config>
(2)范例
4.dao层
(1)代码块
package cn.dao;
import cn.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
/**
* 账户接口类
*/
@Repository("AccountDao")
public interface AccountDao {
//查询所有账户信息
@Select("select * from account")
public List<Account> findAll();
//保存账户信息
@Insert("insert into account (name,money) values (#{name},#{money}) ")
public void saveAccount(Account account);
}
(2)范例
三.总结
spring整合mybatis三要素:
1.配置数据库连接池
2.配置sqlFactory对象
3.配置dao层接口所在的包
声明式事务三要素:
1.事务管理器
2.事务通知
3.配置增强AOP