• SSM整合开发流程


    我的spring是3.2,mybatis是3.4

    1 引入user libarary,我的jar文件如下

    //spring mvc core
    springMVCspring-web-3.2.9.RELEASE.jar
    springMVCspring-webmvc-3.2.9.RELEASE.jar
    
    //spring core
    spring3.2corecommons-logging-1.2.jar
    spring3.2corespring-beans-3.2.9.RELEASE.jar
    spring3.2corespring-context-3.2.9.RELEASE.jar
    spring3.2corespring-core-3.2.9.RELEASE.jar
    spring3.2corespring-expression-3.2.9.RELEASE.jar   //spring自己的表达式语言,如果不用可以不添加
    
    //mybatis core
    mybatis3.4coreasm-5.2.jar
    mybatis3.4corecglib-3.2.5.jar
    mybatis3.4corecommons-logging-1.2.jar
    mybatis3.4corelog4j-1.2.17.jar
    mybatis3.4coremybatis-3.4.4.jar
    
    //DBconnector
    MySQLConnectorc3p0-0.9.1.2.jar
    MySQLConnectormysql-connector-java-5.1.40-bin.jar
    
    //translation
    springTxspring-jdbc-3.2.9.RELEASE.jar
    springTxspring-tx-3.2.9.RELEASE.jar
    
    //AOP
    springAOPaopalliance.jar
    springAOPaspectjrt.jar
    springAOPaspectjweaver.jar
    springAOPspring-aop-3.2.9.RELEASE.jar

    //mybatis spring
    mybatisSpringmybatis-spring-1.3.1.jar

    //json
    jsonjackson-core-asl-1.9.2.jar
    jsonjackson-mapper-asl-1.9.2.jar

    2 创建表文件t_student

    CREATE TABLE t_student(
    id INT PRIMARY KEY AUTO_INCREMENT,
    NAME VARCHAR(20) NOT NULL,
    age INT(3));

    3 创建实体类Student

    package com.huitong.entity;
    
    public class Student {
        
        private Integer sid;
        private String sname;
        private Integer sage;
        
        public Integer getSid() {
            return sid;
        }
        public void setSid(Integer sid) {
            this.sid = sid;
        }
        public String getSname() {
            return sname;
        }
        public void setSname(String sname) {
            this.sname = sname;
        }
        public Integer getSage() {
            return sage;
        }
        public void setSage(Integer sage) {
            this.sage = sage;
        }
    
    }
    View Code

    4配置实体类的映射文件StudentMapper.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.huitong.entity.Student">
        <resultMap type="com.huitong.entity.Student" id="studentMap">
            <id column="id" property="sid"/>
            <result column="name" property="sname"/>
            <result column="age" property="sage"/>
            
        </resultMap>
        
        <insert id="add" parameterType="com.huitong.entity.Student">
            INSERT INTO t_student(NAME, age) VALUES(#{sname},#{sage})
        </insert>
    
    </mapper>
    View Code

    5 Student的 dao/service/action

    //StudentDao
    package com.huitong.dao;
    
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    
    import com.huitong.entity.Student;
    
    public class StudentDao {
        
        private SqlSessionFactory sqlSessionFactory;
        public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
            this.sqlSessionFactory = sqlSessionFactory;
        }
        
        public void add(Student stu) throws Exception{
            SqlSession sqlSession = sqlSessionFactory.openSession();
            sqlSession.insert(Student.class.getName() + ".add", stu);
            sqlSession.close();
        }
    
    }
    
    
    //StudentService
    package com.huitong.service;
    
    import com.huitong.dao.StudentDao;
    import com.huitong.entity.Student;
    
    public class StudentService {
        
        private StudentDao studentDao;
        public void setStudentDao(StudentDao studentDao) {
            this.studentDao = studentDao;
        }
        
        public void add(Student stu) throws Exception{
            studentDao.add(stu);
        }
    
    }
    
    
    //StudentAction
    package com.huitong.action;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.huitong.entity.Student;
    import com.huitong.service.StudentService;
    
    @Controller
    @RequestMapping(value="/student")
    public class StudentAction {
        
        private StudentService StudentService;
        @Resource(name="studentService")
        public void setStudentService(StudentService studentService) {
            StudentService = studentService;
        }
        
        @RequestMapping(value="/register")
        public String register(Student stu, Model model) throws Exception{
            StudentService.add(stu);
            model.addAttribute("student", stu);
            
            return "success";
        }
    }
    View Code

    6 mybatis的配置文件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>
        <mappers>
            <mapper resource="com/huitong/entity/StudentMapper.xml"/>
        </mappers>
    
    </configuration>

    7 spring/spring mvc配置到一个文件中spring.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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
            
            
            <!-- 1 dataSource -->
            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
                <property name="jdbcUrl" value="jdbc:mysql:///day17?useSSL=true"></property>
                <property name="user" value="root"></property>
                <property name="password" value="123456"></property>
                <property name="initialPoolSize" value="3"></property>
                <property name="maxPoolSize" value="10"></property>
                <property name="maxStatements" value="20"></property>
                <property name="acquireIncrement" value="2"></property>
                
            </bean>
            
            <!-- 2 sessionFactory:datasource/xml配置文件 -->
            <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
                <property name="dataSource" ref="dataSource"></property>
                <property name="configLocation" value="classpath:mybatis.xml"></property>
            </bean>
            
            <!-- 3 dao/service/action -->
            <bean id="studentDao" class="com.huitong.dao.StudentDao">
                <property name="sqlSessionFactory" ref="sessionFactory"></property>
            </bean>
            
            <bean id="studentService" class="com.huitong.service.StudentService">
                <property name="studentDao" ref="studentDao"></property>
            </bean>
            
            <!-- <bean id="studentAction" class="com.huitong.action.StudentAction">
                <property name="studentService" ref="studentService"></property>
            </bean> -->
            <!-- 使用扫描方式 -->
            <context:component-scan base-package="com.huitong.action"></context:component-scan>
            
            
            
            <!-- 4 transaction -->
            <!-- 4.1 txManager -->
            <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="dataSource"></property>        
            </bean>
            
            <!-- 4.2 txAdvice -->
            <tx:advice id="txAdvice" transaction-manager="txManager">
                <tx:attributes>
                    <tx:method name="get*" read-only="true"/>
                    <tx:method name="query*" read-only="true"/>
                    <tx:method name="*"/>
                </tx:attributes>
            </tx:advice>
            
            <!-- 4.3 AOP -->
            <aop:config>
                <aop:pointcut expression="execution(* com.huitong.service.*.*(..))" id="pt"/>
                <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
            </aop:config>
            
            
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
                <property name="prefix" value="/jsp/"></property>
                <property name="suffix" value=".jsp"></property>
            </bean>
            
    </beans>

    说明:在spring.xml文件中,SqlSessionFactory说明了mybatis的配置文件位置。也对spring mvc进行了配置,指示了扫描哪些文件,视图处理器。其他的就是spring常规配置了。

    对mapping映射器进行扫描方式。

    如果项目比较大也可以将配置文件进行拆分。

    8 web.xml中配置springmvc核心servlet:dispatcherservlet。字符编码过滤器:CharacterEncodingFilter

    <?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_2_5.xsd" id="WebApp_ID" version="2.5">
        
        <servlet>
            <servlet-name>DispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring.xml</param-value>
            </init-param>
        </servlet>
        
        <servlet-mapping>
            <servlet-name>DispatcherServlet</servlet-name>
            <url-pattern>*.action</url-pattern>
        </servlet-mapping>
        
        <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>
        </filter>
        
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        
        
    </web-app>
    View Code
  • 相关阅读:
    Addrinfo and Getaddrinfo
    网络编程原始套接字
    《福布斯》:IT人最痛苦!?
    Git安装使用笔记 [转]
    Linux下Sniffer程序的实现
    HttpWebRequest post提交XMl参数请求,
    Flex学习记录(一)——MXML基本知识
    利用System.Net.Mail 的SmtpClient发送邮件
    Flex开源开发框架
    C# 手动/自动保存图片
  • 原文地址:https://www.cnblogs.com/zhaopengcheng/p/6869644.html
Copyright © 2020-2023  润新知