首页 > 编程 > Java > 正文

SSM框架搭建+实际java小例子+最主要的配置文件

2019-11-10 16:53:12
字体:
来源:转载
供稿:网友

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>travel</display-name>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>    <filter>        <filter-name>struts</filter-name>        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPRepareAndExecuteFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>struts</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <listener>        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>    </listener>

</web-app>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 --><beans xmlns="http://www.springframework.org/schema/beans"    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:jdbc="http://www.springframework.org/schema/jdbc"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/jdbc      http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <!-- 注解配置 -->    <context:annotation-config />    <!-- 注解配置 :注解哪些包,一般情况下是所有的包 -->    <context:component-scan base-package="cn.com"></context:component-scan>    <!-- 1. 数据源 : DriverManagerDataSource -->    <bean id="dataSource"        class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="com.MySQL.jdbc.Driver" />        <property name="url" value="jdbc:mysql://localhost:3306/travel" />  <!-- 数据库名 -->        <property name="username" value="root" />        <property name="passWord" value="8023" />    </bean>    <!-- 2. mybatis的Sqlsession的工厂: SqlSessionFactoryBean dataSource / typeAliasesPackage -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />  <!-- 依靠上面的数据源(第一点) -->        <property name="typeAliasesPackage" value="cn.com.model" />  <!-- value值为所有的model里面的实体类 -->    </bean>    <!-- 3. mybatis自动扫描加载Sql映射文件 : MapperScannerConfigurer sqlSessionFactory         / basePackage -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="cn.com.dao" />  <!-- value值为所有的dao里面的类 -->        <property name="sqlSessionFactory" ref="sqlSessionFactory" />  <!-- 依靠第二点 -->    </bean>    <!-- 4. 事务管理 : DataSourceTransactionManager -->    <bean id="txManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 5. 使用声明式事务 -->    <tx:annotation-driven transaction-manager="txManager" /></beans>

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="user_manage" extends="struts-default">        <action name="user-*" class="userAction" method="{1}">            <result name="loginSuccess">/index.jsp</result>            <result name="loginError">/login.jsp</result>            <result name="registerSuccess">/login.jsp</result>            <result name="getUserssuccess">/userList.jsp</result>            <result name="editUserSuccess">/index.jsp</result>            <result name="editUserFail">/editUser.jsp</result>            <result name="logoutSuccess">/login.jsp</result>        </action>    </package>        <!-- 旅游产品 -->    <package name="product_manage" extends="struts-default">        <action name="product-*" class="productAction" method="{1}">            <result name="getProductsSuccess">/onsale_0.jsp</result>            <result name="searchProductsSuccess">/onsale_0.jsp</result>            <result name="getHistoryScanSuccess">/onsale_0.jsp</result>            <result name="loginError">/login.jsp</result>            <result name="input">/index.jsp</result>        </action>    </package></struts>

log4j.xml

log4j.properties/uFF0Clog4j.rootLogger=DEBUG, Console#Consolelog4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.layout=org.apache.log4j.PatternLayoutlog4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%nlog4j.logger.java.sql.ResultSet=INFOlog4j.logger.org.apache=INFOlog4j.logger.java.sql.Connection=DEBUGlog4j.logger.java.sql.Statement=DEBUGlog4j.logger.java.sql.PreparedStatement=DEBUG

Action.java

@Scope("request")@Controller("userAction")public class UserAction extends ActionSupport implements ServletRequestAware {    private static final long serialVersionUID = 1L;

......@Autowired    @Qualifier("userService")    private UserService userService;        public HttpServletRequest request;    @Override    public void setServletRequest(HttpServletRequest request) {        this.request = request;    }

......HttpSession session = request.getSession();

dao.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="cn.com.dao.EstimateDao">    <resultMap type="Estimate" id="estimateMap">        <id column="estimate_id" property="estimateId" />        <result column="estimate_content" property="estimateContent" />        <result column="estimate_state" property="estimateState"/>        <association property="user" column="user_id" select="cn.com.dao.UserDao.getUserByUserId"/>    </resultMap>   

    <!-- 根据帖子Id获取该帖子的获赞数 -->    <select id="getGoodNumByTopicId" parameterType="int" resultType="int">        select count(*)         from         travel_estimate         where             topic_id = #{id}         and estimate_content = 'good'     </select></mapper>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表