zoukankan      html  css  js  c++  java
  • 使用ssm框架实现简单网页注册功能

    1、注册Spring配置文件,在web应用启动时创建Spring容器(注册listener)。

    <!-- 注册spring配置文件 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-*.xml</param-value>
        </context-param>
     
        <!--注册contextLoaderListener,在web应用启动时创建spring容器-->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    

    2、字符集过滤器

    <!--字符集过滤器,-->
        <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>
    

    3、注册SpringMVC中央调度器

    <!--MVC中央调度器,注册mvc配置文件-->
        <servlet>
            <servlet-name>SpringMVCDispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>SpringMVCDispatcher</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
    

    4、定义实体类,实现页面。
    例如Student类,注册页面,欢迎页面

    5、实现处理器,编写主业务逻辑,等待service注入。

    public class RegisterHandler implements Controller {
        private IStudentService studentService;//等待注入,设置setter
     
        public void setStudentService(IStudentService studentService) {
            this.studentService = studentService;
        }
     
        @Override
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
            String name = request.getParameter("name");
            String ageStr = request.getParameter("age");
            Integer age = Integer.valueOf(ageStr);
            Student student = new Student(name, age);
            studentService.registerStudent(student);
     
            ModelAndView modelAndView = new ModelAndView("/welcome.jsp");
            modelAndView.addObject("student",student);
            return modelAndView;
        }
    }
    

    6、编写service层接口,等待dao层注入

    public interface IStudentService {
        void registerStudent(Student student);
    }
     
    public class StudentServiceImpl implements IStudentService {
        private IStudentDao studentDao;//等待注入,设置setter
     
        public void setStudentDao(IStudentDao studentDao) {
            this.studentDao = studentDao;
        }
     
        @Override
        public void registerStudent(Student student) {
            studentDao.insertStudent(student);
        }
    }
    

    7、编写dao层接口,实现dao层(mapper映射)。最好把mapper映射放在dao包下,但是这样有个问题就是不会被编译。尝试放在resources文件夹下

    public interface IStudentDao {
        void insertStudent(Student student);
    }
     
    //IStudentDao.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="dao.IStudentDao">
        <insert id="insertStudent" useGeneratedKeys="true" keyProperty="id">
            insert into student(name,age) values(#{name},#{age})
        </insert>
    </mapper>
    

    8、数据库连接,数据源

    //jdbc.properties
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai
    jdbc.user=1234
    jdbc.password=1234
     
    //spring-db.xml
    <!-- c3p0 数据源-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.user}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
        <!--注册数据库配置属性文件-->
        <context:property-placeholder location="classpath:jdbc.properties"/>
    

    9、整合spring与mybatis
    重点:sqlSessionFactoryBean、mapper扫描器

    <!--注册SqlSessionFactory的Bean-->
        <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:mybatis.xml"/>
        </bean>
     
        <!--mapper扫描器,指定扫描mapper映射文件的位置-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
            <property name="basePackage" value="dao"/>
        </bean>
    

    10、dao注入到service,service注入到handler

    <bean id="studentService" class="service.StudentServiceImpl">
            <property name="studentDao" ref="IStudentDao"/>
        </bean>
     
     <bean id="/test/register.do" class="handlers.RegisterHandler">
            <property name="studentService" ref="studentService"/>
        </bean>
    

    11、最后在pom里编译一下没放进resources目录的xml文件

    <build>
            <!--重要!!!-->
            <!--  编译java目录下的xml文件(不在resources目录下的) -->
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </resource>
            </resources>
        </build>
    

    12、添加事务处理

    <!-- 注册事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
     
        <tx:advice id="advice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="register*" propagation="REQUIRED" isolation="DEFAULT"/>
            </tx:attributes>
        </tx:advice>
     
        <!-- AspectJ的aop配置-->
        <aop:config>
            <aop:pointcut id="point" expression="execution(* service.*.*(..))"/>
            <aop:advisor advice-ref="advice" pointcut-ref="point"/>
        </aop:config>
    
  • 相关阅读:
    javascript继承对象冒充
    javascript原型prototype(2)
    javascript继承call()和apply实现继承
    javascript继承原型链继承
    javascript原型prototype(3)
    没有宽高的情况下实现水平垂直居中
    TCP协议
    什么是模块化?模块化的好处是什么?
    数组中嵌套数组,转化为一个数组形式/二维数组转化为一维数组
    常见的请求头类型
  • 原文地址:https://www.cnblogs.com/darknessplus/p/10292305.html
Copyright © 2011-2022 走看看