ssm搭建步骤

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">

 <display-name>66ssm_student</display-name>

<!-- 配置字符编码过滤器 -->

<filter>

    <filter-name>CharacterEncodingFilter</filter-name>   

    <filterclass>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

     <init-param>

        <param-name>forceEncoding</param-name>

        <param-value>true</param-value>

    </init-param>

    <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>

</filter-mapping>

<!-- 配置spring mvc -->

<servlet>

    <servlet-name>springmvc</servlet-name>

    <servletclass>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:spring-*.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

    <servlet-name>springmvc</servlet-name>

    <url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

2.配置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>

        <package name="com.student.entity"/>

    </typeAliases>

    <!-- 设置分页插件 -->

    <plugins>

        <plugininterceptor="com.github.pagehelper.PageInterceptor"></plugin>

    </plugins>

</configuration>

3.配置IStudentMapper.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.student.dao.IStudentMapper">

    <!-- 1.代码片段 -->

    <sql id="allColumns">

        student_id,student_name,student_age,student_gender,student_score,delete_flag,create_time,modify_time,drop_time

    </sql>

    <!-- 2.结果集映射 -->

    <resultMap type="Student" id="studentMap">

        <id column="student_id" property="studentId" />

        <result column="student_name" property="studentName" />

        <result column="student_age" property="studentAge" />

        <result column="student_gender" property="studentGender" />

        <result column="student_score" property="studentScore" />

        <result column="delete_flag" property="deleteFlag" />

        <result column="create_time" property="createTime" />

        <result column="modify_time" property="modifyTime" />

        <result column="drop_time" property="dropTime" />

    </resultMap>


    <insert id="save" parameterType="Student">

        insert into student (student_id,student_name,

        <trim>

            <if test="studentAge != null">student_age,</if>

            <if test="studentGender != null">student_gender,</if>

            <if test="studentScore != null">student_score,</if>

        </trim>

        create_time)

        values(sys_guid(),#{studentName},

        <trim>

            <if test="studentAge != null">#{studentAge},</if>

            <if test="studentGender != null">#{studentGender},</if>

            <if test="studentScore != null">#{studentScore},</if>

        </trim>

        sysdate)

    </insert>


    <update id="remove" parameterType="Student">

        <if test="studentId != null">

            update student set delete_flag = '1' where student_id = #{studentId} and delete_flag = '0'

        </if>

    </update>


    <update id="update" parameterType="Student">

        update student

        <set>

            <if test="studentName != null">student_name=#{studentName},</if>

            <if test="studentAge != null">student_age=#{studentAge},</if>

            <if test="studentGender != null">student_gender=#{studentGender},</if>

            <if test="studentScore != null">student_score=#{studentScore},</if>

        </set>

            where student_id=#{studentId} and delete_flag = '0'

    </update>


    <select id="queryAll" resultMap="studentMap">

        select

        <include refid="allColumns"></include>

        from student

    </select>


    <select id="queryById" resultMap="studentMap" parameterType="String">

        select

        <include refid="allColumns"></include>

        from student

        <where>

            <if test="value != null">student_id=#{studentId}</if>

            and delete_flag = '0'

        </where>

    </select>

</mapper>

4.配置spring-mybatis.xml

<?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:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

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-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.配置数据源 -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>

        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>

        <property name="username" value="suser"></property>

        <property name="password" value="123"></property>

    </bean>

    <!-- 2.配置SqlSessionFactory -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <property name="dataSource" ref="dataSource"></property>

        <property name="configLocation" value="classpath:mybatis.xml"></property>

    </bean>

    <!-- 3.配置mapperScannerconfig -->

    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>

        <property name="basePackage" value="com.student.dao"></property>

    </bean>

    <!-- 事务管理 -->

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource"></property>

    </bean>

    <!-- 事务管理走注释,交给txManager来负责 -->

    <tx:annotation-driven transaction-manager="txManager"/>

</beans>

5.配置spring-context.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-4.0.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-4.0.xsd">

    <!-- 添加扫描包路径 -->

    <context:component-scan base-package="com.student"></context:component-scan>

    <!-- 自动注入对象 -->

    <context:annotation-config></context:annotation-config>

</beans>

6.配置spring-mvc.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-4.0.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-4.0.xsd">

    <!-- 请求映射 -->

    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 拦截器 -->

    <!-- 上传文件 -->

    <!-- 视图解析器 -->

    <!-- 处理静态资源 -->

    <mvc:default-servlet-handler />

</beans>

7.配置log4j.properties

log4j.rootLogger=DEBUG, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%n


log4j.appender.R=org.apache.log4j.DailyRollingFileAppender

log4j.appender.R.File=../logs/service.log

log4j.appender.R.layout=org.apache.log4j.PatternLayout

log4j.appender.R.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%n


log4j.logger.com.ibatis = debug

log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = debug

log4j.logger.com.ibatis.common.jdbc.ScriptRunner = debug

log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = debug

log4j.logger.java.sql.Connection = debug


log4j.logger.java.sql.Statement = debug

log4j.logger.java.sql.PreparedStatement = debug

log4j.logger.java.sql.ResultSet =debug

8.项目结构


9.项目jar包


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。