SSM整合开发流程

我的spring是3.2,mybatis是3.4

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

//spring mvc core
springMVC\spring-web-3.2.9.RELEASE.jar
springMVC\spring-webmvc-3.2.9.RELEASE.jar

//spring core
spring3.2core\commons-logging-1.2.jar
spring3.2core\spring-beans-3.2.9.RELEASE.jar
spring3.2core\spring-context-3.2.9.RELEASE.jar
spring3.2core\spring-core-3.2.9.RELEASE.jar
spring3.2core\spring-expression-3.2.9.RELEASE.jar

//mybatis core
mybatis3.4core\asm-5.2.jar
mybatis3.4core\cglib-3.2.5.jar
mybatis3.4core\commons-logging-1.2.jar
mybatis3.4core\log4j-1.2.17.jar
mybatis3.4core\mybatis-3.4.4.jar

//DBconnector
MySQLConnector\c3p0-0.9.1.2.jar
MySQLConnector\mysql-connector-java-5.1.40-bin.jar

//translation
springTx\spring-jdbc-3.2.9.RELEASE.jar
springTx\spring-tx-3.2.9.RELEASE.jar

//AOP
springAOP\aopalliance.jar
springAOP\aspectjrt.jar
springAOP\aspectjweaver.jar
springAOP\spring-aop-3.2.9.RELEASE.jar

//mybatis spring
mybatisSpring\mybatis-spring-1.3.1.jar

//json
json\jackson-core-asl-1.9.2.jar
json\jackson-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;
    }

}

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>

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

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>

时间: 2024-08-23 04:04:42

SSM整合开发流程的相关文章

Java EE互联网轻量级框架整合开发— SSM框架(中文版带书签)、原书代码

Java EE互联网轻量级框架整合开发 第1部分 入门和技术基础 第1章 认识SSM框架和Redis 2 1.1 Spring框架 2 1.2 MyBatis简介 6 1.3 Spring MVC简介 11 1.4 最流行的NoSQL——Redis 12 1.5 SSM+Redis结构框图及概述 13 第2章 Java设计模式 15 2.1 Java反射技术 15 2.2 动态代理模式和责任链模式 19 2.3 观察者(Observer)模式 30 2.4 工厂模式和抽象工厂模式 35 2.5

java web开发入门八(ssm整合)基于intellig idea

ssm整合 一.导入相关包 二.开发流程 1.写entity package com.eggtwo.euq.entity; import java.io.Serializable; import java.math.BigDecimal; import java.util.Date; public class Member { private int id; public int getId() { return id; } public void setId(int id) { this.id

SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。

SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划的架构组合 Sping MVC + Spring + MyBatis(非Ajax版) Sping MVC + Spring + MyBatis(Ajax版) Sping MVC + Spring + MyBatis(Ajax版 + JavaConfig) Spring Boot + MyBatis

【转】ssm整合

http://m.blog.csdn.net/article/details?id=44455235 SSM框架--详细整合教程(Spring+SpringMVC+MyBatis) 发表于2015/3/19 11:44:55  576280人阅读 分类: Spring MVC 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合的过程,这次刚刚好基于

SSM整合配置

SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis) 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合的过程,这次刚刚好基于自己的一个小项目重新搭建了一次,而且比项目搭建的要更好一些.以前解决问题的过程和方法并没有及时记录,以后在自己的小项目中遇到我再整理分享一下.这次,先说说三大框架整合过程.个人认为使用框架并不是很难

重磅回归-SSM整合进阶项目实战之个人博客系统

历经一个多月的重新设计,需求分析以及前后端开发,终于有了一定的输出:我自己实现的spring4+springmvc+mybatis3整合的进阶项目实战-个人博客系统 已然完成了,系统采用mvc三层模式进行整体的开发,涉及到技术一下子很难全部列出,其中不得不提的有:整合shiro实现登录安全认证,整合lucene实现全文信息检索,基于Spring的事件驱动模型实现业务服务模块之间的异步解耦(在RabbitMQ视频教程中我也会重提这个技术点!),爬虫框架Jsoup解析html文本中的图片,整合ued

EAM系统开发流程

EAM系统开发流程主要包括三个阶段: EAM系统分析 主要是通过规范EAM系统内的信息,进一步把它们整合成一个完整的EAM原型.具体的工作是定义EAM系统中的词汇和建立一组用来生成具有可重用和可配置的概念模型的规范.这些对实施具体系统有指导作用,并且是其基础,类似施工手册.例如,对于资产可以定义为一个能跟踪.修复后可重复使用的.唯一具有独立名称的实体.  EAM系统设计 主要是开发一个高度灵活的通用架构,并且提供一个生产规范.软件构架为组件或对象的重组和配置提供了技术保证,为实现系统的灵活通用提

20个可以帮你简化iOS app开发流程的工具

这里推荐20个可以帮你简化iOS app开发流程的工具.很多开发者都使用过这些工具,涉及原型和设计.编程.测试以及最后的营销,基本上涵盖了整个开发过程. 原型和设计 有了一个很好的创意后,你要做的不是立刻编程,而是设计UI和创建原型,这样你才能知道app如何运行,根据用户体验需要做哪些调整. App Cooker AppCooker 不仅是一个创建原型的优秀工具,它提供的许多功能还可以帮助你将程序发布到App store中.它集成了Dropbox,Box.net和photo roll,你可以直接

从MVC和三层架构说到ssh整合开发-下

这章主要讲整合开发,直接从实战讲起,对与ssh的单方面了解,请继续等待我的兴许文章. 解说不到位的地方欢迎大家指正:联系方式rlovep.com 具体请看源码凝视: 全部代码下载(csdn):链接 Github链接:链接https://github.com/wpeace1212/javaBlog/tree/master/sshDemo 写文章不易,欢迎大家採我的文章,以及给出实用的评论.当然大家也能够关注一下我的github.多谢. 1.整合流程 针对一个简单项目.让大家对三层机构和MVC有一个