一、数据库连接
(1) 使用自定义配置文件连接oracle
文件名:db.propertites
内容:
driver=oracle.jdbc.driver.Oracle.Driver
url=jdbc:oracle:thin:@127.0.0.1:1521:userdb?useUnicode=true&characterEncoding=utf-8
username=root
password=1
不用框架读取配置文件时
private static String driver;
private static String url;
private static String user;
private static String password;
private static Properties pr=new Properties();
private JdbcUtill(){}
//工具类的静态初始化器中的代码,代码在装入类时执行,且只执行一次
static{
try {
pr.load(JdbcUtill.class.getClassLoader().getResourceAsStream("db.properties"));
driver=pr.getProperty("driver");
url=pr.getProperty("url");
user=pr.getProperty("username");
password=pr.getProperty("password");
Class.forName(driver);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
//获取连接
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url, user, password);
}
//释放结果集,语句和连接
public static void free(ResultSet rs,Statement st,Connection conn){
try {
if(rs!=null){
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
(2) 使用自定义配置文件连接mysql
文件名:db.propertites
内容:
jdbc.dirver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///CRM_SSM?useUnicode=true&characterEncoding=UTF-8
jdbc.user=root
jdbc.pwd=1
使用框架读取自定义配置文件
<!-- spring整合数据源连接池 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean name="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.dirver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.pwd}"></property>
</bean>
(3) 使用hibernate在配置文件直接配置
文件名:hibernate.cfg.xml
内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库url -->
<property name="hibernate.connection.url">jdbc:mysql:///user?useUnicode=true&characterEncoding=UTF-8</property>
<!-- 数据库连接用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="hibernate.connection.password">1</property>
<!-- 数据库方言
不同的数据库中,sql语法略有区别. 指定方言可以让hibernate框架在生成sql语句时.针对数据库的方言生成.
sql99标准: DDL 定义语言 库表的增删改查
DCL 控制语言 事务 权限
DML 操纵语言 增删改查
注意: MYSQL在选择方言时,请选择最短的方言.
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 将hibernate生成的sql语句打印到控制台 -->
<property name="hibernate.show_sql">true</property>
<!-- 将hibernate生成的sql语句格式化(语法缩进) -->
<property name="hibernate.format_sql">true</property>
<!--
## auto schema export 自动导出表结构. 自动建表
#hibernate.hbm2ddl.auto create 自动建表.每次框架运行都会创建新的表.以前表将会被覆盖,表数据会丢失.(开发环境中测试使用)
#hibernate.hbm2ddl.auto create-drop 自动建表.每次框架运行结束都会将所有表删除.(开发环境中测试使用)
#hibernate.hbm2ddl.auto update(推荐使用) 自动生成表.如果已经存在不会再生成.如果表有变动.自动更新表(不会删除任何数据).
#hibernate.hbm2ddl.auto validate 校验.不自动生成表.每次启动会校验数据库中表是否正确.校验失败.
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 引入orm元数据
路径书写: 填写src下的路径
-->
<mapping resource="domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
(4) 使用mybatis在配置文件直接配置
文件名:mybatis-config.xml
内容:
<?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>
<environments default="development">
<environment id="development">
<!-- 开启事务 -->
<transactionManager type="JDBC"/>
<!--连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/Mybatis_day01?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="1"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--填写映射文件路径-->
<mapper resource="dao/User.xml"/>
<mapper resource="dao/Orders.xml"/>
</mappers>
</configuration>
二、框架配置文件
1.Struts2(单独)
必须配置两个文件web.xml,struts.xml
(1)文件名:web.xml
添加内容:(统一添加跟版本无关)
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(2)文件名:struts.xml,放于src目录下或新建文件夹config(使用时右击文件-->build path-->source-->add folder-->勾选config-->apply)与src同级目录下,除web.xml所有配置文件位置处理方式与此相同
内容:
命名空间引入(以eclipse)
进入项目libraries--->web app libraries--->找到导入的struts2核心jar包struts2-core--->直接在该目录下找到最新版本的dtd(这里最新以2.3为例)-->进入dtd文件找到命名空间复制到struts.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="add" class="Action.DemoAction">
<result name="+">/positive.jsp</result>
<result name="-">/negative.jsp</result>
</action>
</package>
</struts>
2.Hibernate(单独)
必须配置至少两个文件:hibernate.cfg.xml(配置文件,必须),xxx.hbm.xml(映射文件,由pojo对象决定),无需配置web.xml
(1)文件名:hibernate.cfg.xml
内容:
命名空间引入(以eclipse)
进入项目libraries--->web app libraries--->找到导入的hibernate核心jar包hibernate-core--->org.hibernate--->在该目录下找到configuration.dtd(hibernate的配置文件)-->进入dtd文件找到命名空间复制到hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!--数据库连接代码见上面-->
</hibernate-configuration>
(2)文件名:xxx.hbm.xml
内容:
命名空间引入(以eclipse)
进入项目libraries--->web app libraries--->找到导入的hibernate核心jar包hibernate-core--->org.hibernate--->在该目录下找到mapping.dtd(hibernate的映射文件)-->进入dtd文件找到命名空间复制到xxx.hbm.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-mapping package="domain">
<class name="User">
<id name="id">
<generator class="increment" />
</id>
<property name="name" />
</class>
</hibernate-mapping>
3.Mybatis(单独)
必须配置至少两个文件:mybatis-config.xml(配置文件,必须),xxx.xml(映射文件,由pojo对象决定),无需配置web.xml
(1) 文件名:mybatis-config.xml
内容:
命名空间引入
进入官方文档http://www.mybatis.org/mybatis-3/zh/index.html查找
<?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>
</configuration>
(2) 文件名:xxx.xml
内容:
命名空间引入
进入官方文档http://www.mybatis.org/mybatis-3/zh/index.html查找
<?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">
<mapper namespace="dao.IOrdersDao">
<select id="selectOrders1" resultType="pojo.Orders">
select id,userId as userid,number,createTime,note from orders where id=1
</select>
<!-- 自定义映射规则 -->
<resultMap id="myResultMap" type="pojo.Orders">
<!-- 配置主键映射 -->
<id property="id" column="id" />
<!-- 配置普通列映射 userId变成userid-->
<result property="userId" column="userid" />
</resultMap>
<select id="selectOrders2" resultMap="myResultMap">
select id,userId,number,createTime,note from orders where id=2
</select>
<resultMap type="pojo.Orders" id="orderMap">
<!-- 因为Orders和User都有id未避免冲突取别名 -->
<id property="id" column="oid"/>
<result property="number" column="number"/>
<!-- 关联一个对象 -->
<association property="user" javaType="pojo.User">
<id property="id" column="uid"/>
<result property="username" column="username"/>
</association>
</resultMap>
<select id="selectOrdersAndUser" parameterType="Integer" resultMap="orderMap">
SELECT u.id AS uid,u.username,o.id AS oid, o.number
FROM USER u,orders o
WHERE u.id=o.userId and o.id=#{id}
</select>
</mapper>
4.Spring(单独)
至少配置一个文件applicationContext.xml(名字任取,尽量规范),可以创建多个xml管理的不同层,这里用一个xml来举例
(1)文件名:applicationContext.xml
内容:
命名空间引入(以eclipse)
创建完applicationContext.xml文件后,写一对beans标签,进入xml的design模式,右击--->edit namespace--->add--->勾选xsi,默认选择select from registered namespaces,点击ok--->再次add--->选择specify new namespace--->先填location hint,点击browser--->选择select xml catelog entry,找到对应xsd
这里以bean和context为例截图
beans无需填写prefix
context要填写prefix
完成上述操作要保证开发环境之前导入相应的xsd文件
这里介绍如何导入xsd文件
Window--->preferences--->搜索框输入catalog,找到xml catalog--->add--->见截图以spring.util.xsd为例
点击file system
找到存放spring约束的位置,不用选择太高版本4.0即可,这样导入的包4.0还是4.0以上都能用
Key type选择schema location,并且在key最后加上spring-util-4.o.xsd
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<!--
扫描包:否则无法自动注入对象
-->
<context:component-scan base-package="pojo"></context:component-scan>
<context:component-scan base-package="service"></context:component-scan>
<context:component-scan base-package="aop"></context:component-scan>
<context:component-scan base-package="test"></context:component-scan>
<!-- 配置spring管理User对象 -->
<!--
方法一:默认使用无参构造
-->
<bean name="user1" class="pojo.User"></bean>
<!--
方法二:工厂静态方法,直接调用静态方法可以返回Bean实例
-->
<bean name="user2" class="utils.BeanFactoryUtil" factory-method="getUser2"></bean>
<!--
方法三:工厂动态方法,先调用工厂本身,再调用工厂的实例方法来返回bean实例
-->
<bean name="f" class="utils.BeanFactoryUtil"></bean>
<bean name="user3" factory-bean="f" factory-method="getUser1"></bean>
<!--
bean属性的详解
name:创建对象的名字,也可以使用id
class:类的完整路径名
init-method:对象初始化完成调用的方法
destroy-method:容器正常关闭之前调用的方法
scope:
singleton:单例
prototype:多例
request:和request生命周期一样
session:和session生命周期一样
-->
<bean id="user" init-method="testInit" destroy-method="testDestroy" scope="singleton" class="pojo.User"></bean>
<!--
set方法注入,默认走set方法注入值(基本类型都可以注入)
-->
<bean name="department" class="pojo.Department">
<property name="id" value="1"></property>
<property name="name" value="开发部"></property>
</bean>
<bean name="user4" class="pojo.User">
<property name="age" value="18"></property>
<property name="name" value="赵四"></property>
<!-- 使用ref标签引用另一个bean对象 -->
<property name="department" ref="department"></property>
</bean>
<!--
构造参数注入
-->
<bean name="user5" class="pojo.User">
<constructor-arg name="age" value="20"></constructor-arg>
<constructor-arg name="name" value="刘能"></constructor-arg>
</bean>
</beans>
补充:介绍一下dtd和xsd文件的区别
dtd是使用非xml语法编写的,dtd是不可扩展,不支持命名空间,只提供非常有限的数据类型;
xsd是dtd的替代者,可扩展,比dtd丰富有用,支持命名空间,支持数据类型
5.SpringMVC(单独)
必须配置两个文件web.xml,springmvc.xml(名称任意,尽量规范)
(1)文件名:web.xml
添加内容:(统一添加跟版本无关)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringmvcDemo1App</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
contextConfigLocation:
给DispatcherServlet读取springmvc配置文件
如果不指定加载springmvc配置文件
默认找<servlet名称>-servlet.xml
dispatcherServlet-servlet.xml
-->
<init-param>
<!-- 参数名固定必须写 contextConfigLocation-->
<param-name>contextConfigLocation</param-name>
<!-- 要加classpath: -->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc1</servlet-name>
<!--
*.do(拦截*.do),*.action,*.xx
/:拦截(除了.jsp),(png,css,js,gif,flash,*.do,*.action,*.xx...)
/*:拦截所有,(jsp,png,css,js,gif,flash,*.do,*.action,*.xx...)(不推荐使用)
-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
(2)文件名:springmvc.xml
内容:
命名空间引入与Spring相同详细见上面
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd ">
<!-- 配置springmvc扫描Controller注解 -->
<context:component-scan base-package="com.ncu.controller"></context:component-scan>
<!-- springmvc中三大组件配置 处理器映射器,处理器适配器,视图解析器-->
<!--
处理器映射器 3.2以后必须配置
<bean class="org.springframework.web.servlet.mvc.annotation.RequestMappingHandlerMapping"></bean>
-->
<!--
处理器适配器 3.2以后必须配置
<bean class="org.springframework.web.servlet.mvc.annotation.RequestMappingHandlerMapping"></bean>
-->
<!-- 最终配置 注解驱动,代替处理器适配器+处理器映射器 -->
<mvc:annotation-driven/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--
prefix:自动补充到逻辑视图名的前缀
suffix:自动补充到逻辑视图名的后缀
-->
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
6.Spring整合SpringMVC和Mybatis(SSM),外加整合日志
加web.xml需要配置八个文件(实际可能更多)
(1)文件名:web.xml
内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SSMDemo1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置项目启动加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
(2)文件名:db.properties
内容:见上面
(3)文件名:log4j.properties
内容:
# Global logging configuration
log4j.rootLogger=debug, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
(4)文件名:springmvc.xml
内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd ">
<!-- 配置springmvc扫描Controller注解 -->
<context:component-scan base-package="com.ncu.controller"></context:component-scan>
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
-->
<!-- 最终配置 注解驱动,代替处理器适配器+处理器映射器,高级参数绑定-->
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
(5)文件名:spring-mybatis.xml
内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">
<!-- 1.配置连接池数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean name="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.dirver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.pwd}"></property>
</bean>
<!-- 2.spring整合mybatis工厂 -->
<bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
</bean>
<!-- 3.接管mybatis动态代理对象 -->
<bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ncu.dao"></property>
</bean>
</beans>
(6)文件名:spring-service.xml
内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">
<!-- 管理service层对象 -->
<context:component-scan base-package="com.ncu.service"></context:component-scan>
</beans>
(7)文件名:spring-trans.xml
内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">
<!-- 配置spring管理service层事务 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"></property>
</bean>
<tx:annotation-driven/>
</beans>
(8)文件名:sqlMapCofig.xml
内容:
<?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>
<typeAliases>
<!-- <package name="com.ncu.pojo"/> -->
</typeAliases>
</configuration>
7.Spring整合Struts2和Hibernate
加web.xml需要配置五个文件(为了方便将分层管理都放在applicationContext.xml)
(1)文件名:web.xml
内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
(2)文件名:jdbc.properties
内容:(与db.properties相同,详细同上)
(3)文件名:log4j.properties
内容:(详细同上)
(4)文件名:struts.xml
内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<package name="actions" extends="struts-default" namespace="/">
<action name="user_*" method="{1}" class="userAction">
<result name="showFine">/fineList.jsp</result>
<result name="showAll">/userList.jsp</result>
<result name="addSucceed">/login.jsp</result>
<result name="addFailed">/register.jsp</result>
<result name="loginSucceed">/index.jsp</result>
<result name="loginFailed">/login.jsp</result>
<result name="editPass">/editPassword.jsp</result>
<result name="logout">/login.jsp</result>
<result name="delete" type="redirectAction">
<param name="actionName">user_showAll</param>
</result>
<allowed-methods>showFine,showAll,add,delete,login,editPass,logout</allowed-methods>
</action>
<action name="book_*" method="{1}" class="bookAction">
<result name="showAll2">/admin_bookList.jsp</result>
<result name="add">/addBook.jsp</result>
<result name="showByType">/searchbook.jsp</result>
<result name="showByKey">/searchbook.jsp</result>
<result name="showByHot">/searchbook.jsp</result>
<result name="showAll">/user_bookList.jsp</result>
<result name="searchBook">/searchbook.jsp</result>
<result name="delete" type="redirectAction">
<param name="actionName">book_showAll2</param>
</result>
<allowed-methods>showAll,add,delete,showAll2,showByType,showByKey,showByHot,findBookInfoById</allowed-methods>
</action>
<action name="data_*" method="{1}" class="dataAction">
<result name="showAll">/givebackList.jsp</result>
<result name="showByUser">/givebackList.jsp</result>
<result name="showByUserAndRenew">/renew.jsp</result>
<result name="add" type="redirectAction">
<param name="actionName">data_showByUser</param>
</result>
<result name="delete" type="redirectAction">
<param name="actionName">data_showByUser</param>
</result>
<result name="delete2" type="redirectAction">
<param name="actionName">data_showAll</param>
</result>
<result name="updateDate" type="redirectAction">
<param name="actionName">data_showByUser</param>
</result>
<allowed-methods>showAll,showByUserAndRenew,add,delete,showByUser,updateDate</allowed-methods>
</action>
</package>
</struts>
(4)文件名:applicationContext.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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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">
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="DriverClass" value="${jdbc.driverClass}"></property>
<property name="JdbcUrl" value="${jdbc.url}"></property>
<property name="User" value="${jdbc.username}"></property>
<property name="Password" value="${jdbc.password}"></property>
<property name="testConnectionOnCheckin" value="true"></property>
<property name="idleConnectionTestPeriod" value="60"></property>
</bean>
<!-- 配置hibernate相关属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.autoReconnect">true</prop>
</props>
</property>
<!-- 加载映射文件 -->
<property name="mappingResources">
<list>
<value>entity/User.hbm.xml</value>
<value>entity/Data.hbm.xml</value>
<value>entity/Book.hbm.xml</value>
</list>
</property>
</bean>
<!-- action -->
<bean id="userAction" class="action.UserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<bean id="bookAction" class="action.BookAction" scope="prototype">
<property name="bookService" ref="bookService"></property>
</bean>
<bean id="dataAction" class="action.DataAction" scope="prototype">
<property name="dataService" ref="dataService"></property>
</bean>
<!-- service -->
<bean id="userService" class="service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="dataService" class="service.DataService">
<property name="dataDao" ref="dataDao"></property>
<property name="bookDao" ref="bookDao"></property>
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="bookService" class="service.BookService">
<property name="bookDao" ref="bookDao"></property>
</bean>
<!-- dao -->
<bean id="userDao" class="dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="bookDao" class="dao.BookDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="dataDao" class="dao.DataDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- tx -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启注解tx -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>