SSM整合
- 导入依赖
-
- Spring的依赖
- SpringMVC的依赖
- MyBaits的依赖
- 其他---数据源和驱动
- 补充jsp两个依赖
- 写配置
-
- web.xml配置
- SpringMVC的配置
- 整合步骤
-
- 1.导入整合包,将dao的实现加入到容器中
- MyBaits与Spring整合的关键配置
- Spring配置
- MyBaits配置----这个其实可有可无
- 基础测试的完整代码
-
- 目录结构
- controller层-----页面跳转与转发操作
-
- LockController类
- Dao----操作数据库-----使用mybaits后,这里是一个接口,mybaits来实现该接口
-
- lockDao接口类
- POJO层----存放需要封装数据库字段的类,封装类不需要放入容器中
-
- lock类
- service层-----完成对Dao层操作数据库方法的调用
-
- LockService类
- MyBaits----存放mybaits相关的配置文件
-
- mapper---用来存放实现数据库操作接口的配置文件,使用注解可不用写
-
- lockDao.xml----实现lockDao接口
- mybaits-config.xml----存放mybaits核心配置,其实可有可无
- Spring----存放spring相关配置文件
-
- applicationContext.xml
- SpringMVC-----存放springmvc相关的配置文件
-
- springmvc.xml
- jdbc.properties---数据库配置文件
- 页面就不展示了,下面展示环境搭建后的测试结果
- 一个整合的优化操作
导入依赖
Spring的依赖
<!--导入spring的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--导入aspectj的坐标-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<!--引入Spring测试坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--junit坐标-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
<!--spring tx的坐标,处理事务的-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--spring jdbc的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
SpringMVC的依赖
<!--SpringWeb模块相关的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--jstl的依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--标准标签库的依赖-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- maven管理引入springmvc注解数据校验所需jar包:-->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>
<!--文件上传的依赖-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!--引入jquery的依赖-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1-2</version>
</dependency>
<!--jackSon的依赖===>ajax-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
MyBaits的依赖
<!--MyBaits依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!--log4j日志依赖-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
<!--ehcache第三方专业缓存框架的依赖-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.8</version>
</dependency>
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.3</version>
</dependency>
<!--除了log4j,还需要导入slf4j-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
<scope>test</scope>
</dependency>
其他—数据源和驱动
<!--mysql驱动的坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!--c3p0数据库连接池的坐标-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--druid数据库连接池坐标-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
补充jsp两个依赖
<!--jsp相关依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
写配置
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--配置Spring容器启动-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--指定spring配置文件位置-->
<param-value>classpath:Spring/applicationContext.xml</param-value>
</context-param>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置springmvc的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--两个标准配置-->
<!--字符编码-->
<!--配置一个字符编码的Filter-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--支持REST风格的filter-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<!--拦截所有请求-->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
SpringMVC的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--SpringMVC只扫描控制器:禁用默认过滤规则-->
<context:component-scan base-package="Com" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--文件上传解析器: id必须是multipartFile-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--设置文件上传最大量为5m-->
<property name="maxUploadSize" value="5242880"></property><!--spel运算符,计算最大可上传文件体积-->
<!--设置默认的编码-->
<property name="defaultEncoding" value="utf-8"></property>
</bean>
<!--扫描静态资源-->
<mvc:default-servlet-handler/>
<!--扫描动态资源-->
<mvc:annotation-driven/>
</beans>
整合步骤
1.导入整合包,将dao的实现加入到容器中
<!--mybaits和spring整合包的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
MyBaits与Spring整合的关键配置
<!--配置使用Mybaits操作数据库-->
<!--可以根据配置文件得到SqlSessionFactory-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定配置文件位置·-->
<property name="configLocation" value="classpath:MyBaits/mybaits-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<!-- 指定xml映射文件位置--> <!--所有xml都是我们的映射文件-->
<property name="mapperLocations" value="classpath:MyBaits/mapper/*.xml"></property>
</bean>
<!--我们要把dao接口的实现加入到ioc容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定dao接口所在的包-->
<property name="basePackage" value="Com.Dao"></property>
</bean>
Spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--spring除了控制器不要,剩下的业务逻辑组件都要,包括dao,包括service-->
<context:component-scan base-package="Com">
<!--扫描排除不写use,使用默认扫描规则-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--加载外部的properties文件-->
<!--当前要加载的properties文件在资源文件下,前面需要加上classpath:-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--通过键值的方式引入值-->
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
</bean>
<!--配置使用Mybaits操作数据库-->
<!--可以根据配置文件得到SqlSessionFactory-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定配置文件位置·-->
<property name="configLocation" value="classpath:MyBaits/mybaits-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<!-- 指定xml映射文件位置--> <!--所有xml都是我们的映射文件-->
<property name="mapperLocations" value="classpath:MyBaits/mapper/*.xml"></property>
</bean>
<!--我们要把dao接口的实现加入到ioc容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定dao接口所在的包-->
<property name="basePackage" value="Com.Dao"></property>
</bean>
<!--配置事务控制,配置事务管理器,控制住数据源里面的链接的关闭和提交-->
<!--创建事务管理器对象-->
<!--需要导入aspectj的坐标,即面向切面编程的坐标-->
<!--这里的id可以随便写-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--操作事务需要连接对象,连接对象在连接池中(数据源)-->
<!--控制数据源,通过操作connection连接,来进行事务的回滚,自动提交操作-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--基于xml配置事务,哪些方法切入事务还需要写切入点表达式-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="tx" expression="execution(* Com.service.*.*(..))"/>
<aop:advisor advice-ref="myTx" pointcut-ref="tx"/>
</aop:config>
<!--配置事务增强,事务属性,事务建议-->
<tx:advice id="myTx" transaction-manager="transactionManager">
<tx:attributes>
<!--所有方法都是事务方法,任何事务方法出现任何异常都回滚-->
<tx:method name="*" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
</beans>
MyBaits配置----这个其实可有可无
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--开启延迟加载开关-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--开启属性按需加载-->
<setting name="aggressiveLazyLoading" value="false"/>
<!--开启全局缓存开关-->
<setting name="cacheEnabled" value="true"/>
</settings>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
基础测试的完整代码
目录结构
controller层-----页面跳转与转发操作
LockController类
@Controller
public class LockController
{
@Autowired
private LockService lockService;
@RequestMapping("/hello")
public String getLock(@RequestParam(value = "id",defaultValue = "1") Integer id
, Model model)
{
lock l = lockService.getLockById(id);
model.addAttribute("lock",l);
return "success";
}
}
Dao----操作数据库-----使用mybaits后,这里是一个接口,mybaits来实现该接口
lockDao接口类
//我们这里需要注入的是lockDao接口的实现,而不是lockDao接口
public interface lockDao {
//根据id得到锁
public lock getLockById(Integer id);
}
POJO层----存放需要封装数据库字段的类,封装类不需要放入容器中
lock类
public class lock {
private Integer id;
private String lockName;
private Integer price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLockName() {
return lockName;
}
public void setLockName(String lockName) {
this.lockName = lockName;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public String toString() {
return "lock{" +
"id=" + id +
", lockName='" + lockName + '\'' +
", price=" + price +
'}';
}
}
service层-----完成对Dao层操作数据库方法的调用
LockService类
@Service
public class LockService
{
@Autowired
private lockDao lockdao;
public lock getLockById(Integer id)
{
return lockdao.getLockById(id);
}
}
MyBaits----存放mybaits相关的配置文件
mapper—用来存放实现数据库操作接口的配置文件,使用注解可不用写
lockDao.xml----实现lockDao接口
<?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: 名称空间:写接口的全类名,相当于告诉mybaits这个配置文件是实现哪个接口的-->
<mapper namespace="Com.Dao.lockDao">
<select id="getLockById" resultType="Com.POJO.lock">
SELECT * FROM `lock` WHERE id=#{id};
</select>
</mapper>
mybaits-config.xml----存放mybaits核心配置,其实可有可无
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--开启延迟加载开关-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--开启属性按需加载-->
<setting name="aggressiveLazyLoading" value="false"/>
<!--开启全局缓存开关-->
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
Spring----存放spring相关配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--spring除了控制器不要,剩下的业务逻辑组件都要,包括dao,包括service-->
<context:component-scan base-package="Com">
<!--扫描排除不写use,使用默认扫描规则-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--加载外部的properties文件-->
<!--当前要加载的properties文件在资源文件下,前面需要加上classpath:-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--通过键值的方式引入值-->
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
</bean>
<!--配置使用Mybaits操作数据库-->
<!--可以根据配置文件得到SqlSessionFactory-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定配置文件位置·-->
<property name="configLocation" value="classpath:MyBaits/mybaits-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<!-- 指定xml映射文件位置--> <!--所有xml都是我们的映射文件-->
<property name="mapperLocations" value="classpath:MyBaits/mapper/*.xml"></property>
</bean>
<!--我们要把dao接口的实现加入到ioc容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定dao接口所在的包-->
<property name="basePackage" value="Com.Dao"></property>
</bean>
<!--配置事务控制,配置事务管理器,控制住数据源里面的链接的关闭和提交-->
<!--创建事务管理器对象-->
<!--需要导入aspectj的坐标,即面向切面编程的坐标-->
<!--这里的id可以随便写-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--操作事务需要连接对象,连接对象在连接池中(数据源)-->
<!--控制数据源,通过操作connection连接,来进行事务的回滚,自动提交操作-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--基于xml配置事务,哪些方法切入事务还需要写切入点表达式-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="tx" expression="execution(* Com.service.*.*(..))"/>
<aop:advisor advice-ref="myTx" pointcut-ref="tx"/>
</aop:config>
<!--配置事务增强,事务属性,事务建议-->
<tx:advice id="myTx" transaction-manager="transactionManager">
<tx:attributes>
<!--所有方法都是事务方法,任何事务方法出现任何异常都回滚-->
<tx:method name="*" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
</beans>
SpringMVC-----存放springmvc相关的配置文件
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--SpringMVC只扫描控制器:禁用默认过滤规则-->
<context:component-scan base-package="Com" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--文件上传解析器: id必须是multipartFile-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--设置文件上传最大量为5m-->
<property name="maxUploadSize" value="5242880"></property><!--spel运算符,计算最大可上传文件体积-->
<!--设置默认的编码-->
<property name="defaultEncoding" value="utf-8"></property>
</bean>
<!--扫描静态资源-->
<mvc:default-servlet-handler/>
<!--扫描动态资源-->
<mvc:annotation-driven/>
</beans>
jdbc.properties—数据库配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/tx
jdbc.username=root
jdbc.password=xxxxxx
jdbc.maxPoolSize=20
jdbc.minPoolSize=5
页面就不展示了,下面展示环境搭建后的测试结果
一个整合的优化操作
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<!--spring除了控制器不要,剩下的业务逻辑组件都要,包括dao,包括service-->
<context:component-scan base-package="Com">
<!--扫描排除不写use,使用默认扫描规则-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--加载外部的properties文件-->
<!--当前要加载的properties文件在资源文件下,前面需要加上classpath:-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--通过键值的方式引入值-->
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
</bean>
<!--配置使用Mybaits操作数据库-->
<!--可以根据配置文件得到SqlSessionFactory-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定配置文件位置·-->
<property name="configLocation" value="classpath:MyBaits/mybaits-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<!-- 指定xml映射文件位置--> <!--所有xml都是我们的映射文件-->
<property name="mapperLocations" value="classpath:MyBaits/mapper/*.xml"></property>
</bean>
<!--我们要把dao接口的实现加入到ioc容器-->
<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
<!--指定dao接口所在的包-->
<!-- <property name="basePackage" value="Com.Dao"></property>-->
<!-- </bean>-->
<mybatis:scan base-package="Com.Dao"/>
<!--配置事务控制,配置事务管理器,控制住数据源里面的链接的关闭和提交-->
<!--创建事务管理器对象-->
<!--需要导入aspectj的坐标,即面向切面编程的坐标-->
<!--这里的id可以随便写-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--操作事务需要连接对象,连接对象在连接池中(数据源)-->
<!--控制数据源,通过操作connection连接,来进行事务的回滚,自动提交操作-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--基于xml配置事务,哪些方法切入事务还需要写切入点表达式-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="tx" expression="execution(* Com.service.*.*(..))"/>
<aop:advisor advice-ref="myTx" pointcut-ref="tx"/>
</aop:config>
<!--配置事务增强,事务属性,事务建议-->
<tx:advice id="myTx" transaction-manager="transactionManager">
<tx:attributes>
<!--所有方法都是事务方法,任何事务方法出现任何异常都回滚-->
<tx:method name="*" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
</beans>