公司 使用框架选用 nutz,好久没有使用 市面上常用的框架了,今日 半日偷闲 从网上找了点资料 整理一下,配置一个比较简单的 框架
第一步:配置 web.xml 项目的入口
框架作用:
1.引入 Spring 文件 2.引入Struts.xml(任务分发器)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring配置文件加载的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 配置struts2 -->
<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>
</web-app>
第二步:配置 Spring文件 (此处定义为-bean.xml) 主要包括配置数据源、事务、mybaits等
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 采用注释的方式配置bean spring 容器-->
<context:annotation-config />
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.pdsu.edu"></context:component-scan>
<!--proxy-target-class="true"强制使用cglib代理 如果为false则spring会自动选择-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 队列中的最小等待数 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 队列中的最大等待数 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 最长等待时间,单位毫秒 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
<!-- 最大活跃数 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
</bean>
<!-- 配置mybitasSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis.xml"></property>
</bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
第三步:配置 mybatis.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>
<typeAlias alias="user" type="com.pdsu.edu.domain.User"/>
</typeAliases>
<mappers>
<mapper resource="com/pdsu/edu/domain/sqlMappers/user.xml" />
</mappers>
</configuration>
第四步:配置 专属的 sql映射文件 -user.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">
<mapper namespace="com.pdsu.edu.domain.User">
<resultMap type="com.pdsu.edu.domain.User" id="userResult">
<result property="id" column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<result property="username" column="username" />
<result property="password" column="password" />
</resultMap>
<select id="userLogin" parameterType="user" resultMap="userResult">
select * from user
where
username=#{username} and password=#{password}
</select>
<select id="selectAllUser" resultMap="userResult">
select * from user
</select>
<select id="findUserById" parameterType="int" resultMap="userResult">
select *
from user where id=#{id}
</select>
<insert id="insertUser" parameterType="user">
<![CDATA[
insert into
user(username,password) values(#{username},#{password})
]]>
</insert>
<update id="updateUser" parameterType="user">
update user set
username=#{username},password=#{password} where id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where
id=#{id}
</delete>
</mapper>
第五步:配置struts.xml 任务分发
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 指定默认编码集 ,作用于HttpServletRequest的setCharacterEncoding()和freemarker,vilocity的输出 -->
<constant name="struts.configuration.xmlreload" value="true"/>
<!-- 当struts配置文件修改时是否自动加载 -->
<constant name="struts.devMode" value="true"/>
<!-- 开发模式下打印详细的错误信息 -->
<constant name="struts.ui.theme" value="xhtml"/>
<package name="user" namespace="/user" extends="struts-default">
<action name="user_*" class="userAction" method="{1}">
<result name="success" type="redirectAction">user_queryAllUser.action</result>
<result name="input">/index.jsp</result>
<result name="userList">/userList.jsp</result>
<result name="addUser">/userAdd.jsp</result>
<result name="updateUser">/userUpdate.jsp</result>
</action>
</package>
</struts>
其他的配置文件:
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/operationLog
jdbc.username=root
jdbc.password=
jdbc.maxActive = 2
jdbc.maxIdle =5
jdbc.minIdle=1
jdbc.initialSize =3
jdbc.maxWait =3000
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug, stdout
log4j.logger.java.sql.PreparedStatement=debug
参考文章:
去掉了 service层,直接用dao层接口映射
http://www.cnblogs.com/jyh317/p/3834142.html
资料来源:
http://blog.csdn.net/afgasdg/article/details/7392367