SSM整合开发流程
我的spring是3.2,mybatis是3.4
1 引入user libarary,我的jar文件如下
- //spring mvc core
- springMVC\spring-web-3.2..RELEASE.jar
- springMVC\spring-webmvc-3.2..RELEASE.jar
- //spring core
- spring3.2core\commons-logging-1.2.jar
- spring3.2core\spring-beans-3.2..RELEASE.jar
- spring3.2core\spring-context-3.2..RELEASE.jar
- spring3.2core\spring-core-3.2..RELEASE.jar
- spring3.2core\spring-expression-3.2..RELEASE.jar //spring自己的表达式语言,如果不用可以不添加
- //mybatis core
- mybatis3.4core\asm-5.2.jar
- mybatis3.4core\cglib-3.2..jar
- mybatis3.4core\commons-logging-1.2.jar
- mybatis3.4core\log4j-1.2..jar
- mybatis3.4core\mybatis-3.4..jar
- //DBconnector
- MySQLConnector\c3p0-0.9.1.2.jar
- MySQLConnector\mysql-connector-java-5.1.-bin.jar
- //translation
- springTx\spring-jdbc-3.2..RELEASE.jar
- springTx\spring-tx-3.2..RELEASE.jar
- //AOP
- springAOP\aopalliance.jar
- springAOP\aspectjrt.jar
- springAOP\aspectjweaver.jar
- springAOP\spring-aop-3.2..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() NOT NULL,
- age INT());
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">
- <!-- 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=""></property>
- <property name="initialPoolSize" value=""></property>
- <property name="maxPoolSize" value=""></property>
- <property name="maxStatements" value=""></property>
- <property name="acquireIncrement" value=""></property>
- </bean>
- <!-- 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>
- <!-- 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>
- <!-- 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-</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
SSM整合开发流程的更多相关文章
- SSM(Spring+SpringMVC+MyBatis)框架整合开发流程
回忆了 Spring.SpringMVC.MyBatis 框架整合,完善一个小demo,包括基本的增删改查功能. 开发环境 IDEA MySQL 5.7 Tomcat 9 Maven 3.2.5 需要 ...
- SSM整合开发——基于SSM的OA系统
一.课程介绍 链接: https://pan.baidu.com/s/18B-lWfOUnKZPvuVEHY_NmQ 提取码: ky7t 复制这段内容后打开百度网盘手机App,操作更方便哦 需要 to ...
- 开发步骤Dubbo、spring mvc、springboot、SSM整合开发步骤
一.Dubbo开发步骤: 链接:https://pan.baidu.com/s/1pMPO1kf 密码:9zaa 第一: 1.创建consumer工程2.在pom.xml文件下添加配置3.添加appl ...
- SSM整合开发
导入开发包 asm-3.2.0.RELEASE.jar asm-3.3.1.jar c3p0-0.9.jar cglib-2.2.2.jar com.springsource.net.sf.cglib ...
- java web开发入门八(ssm整合)基于intellig idea
ssm整合 一.导入相关包 二.开发流程 1.写entity package com.eggtwo.euq.entity; import java.io.Serializable; import ja ...
- Java EE互联网轻量级框架整合开发— SSM框架(中文版带书签)、原书代码
Java EE互联网轻量级框架整合开发 第1部分 入门和技术基础 第1章 认识SSM框架和Redis 2 1.1 Spring框架 2 1.2 MyBatis简介 6 1.3 Spring MVC简介 ...
- SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。
SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...
- 本人亲测-SSM整合后的基础包(供新手学习使用,可在本基础上进行二次开发)
本案例是在eclipse上进行开发的,解压后直接添加到eclipse即可.还需要自己配置maven环境.链接:https://pan.baidu.com/s/1siuvhCJASuZG_jqY5utP ...
- ssm整合说明与模板-Spring Spring MVC Mybatis整合开发
ssm整合说明 spring+spring mvc+mybatis 说明 源码下载 由于之前存在ssh框架,spring+struts+hibernate,其中spring负责aop与ioc,所以一般 ...
随机推荐
- 【转】Geary's C
李旭,Matlab: Geary's C 原文地址 Introduction Geary's C is a measure of spatial autocorrelation or an attem ...
- Setup JIRA Software 7.6.2 on Oracle Linux 6.8
OS Oracle Linux 6.8 V138414-01.iso Database mysql5.6.30 MySQL-5.6.30-1.el6.x86_64.rpm-bundle.tar JIR ...
- cmd复制粘贴
右击菜单栏,选择“快速编辑模式” 复制:选择文本后按回车,然后就可以去其他地方粘贴了 粘贴:右击鼠标就可以粘贴内容 简单到都不好意思发布出来了....
- Sqlserver数据库还原.bak文件失败的两个问题
一.SQL Server数据库备份还原后,在数据库名称后会出现“受限制访问”字样 解决方案:将数据库限制访问改为:SINGLE_USER 数据库-->属性-->选项-->状 ...
- ylbtech-LanguageSamples-NamedAndOptional(命名和可选参数)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-NamedAndOptional(命名和可选参数) 1.A,示例(Sample) 返回顶 ...
- 判断客户端是PC还是手持设备的JS代码【转】
1.第一种: 复制代码代码如下: function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = [&quo ...
- Linux pci驱动源码
#include <linux/kernel.h>#include <linux/errno.h>#include <linux/module.h>#include ...
- PostgreSQL 9.5,带来 UPSERT 等新特性
PostgreSQL 9.5于2016年1月7日正式发布,此版本主要带来了以下几个方面的特性: UPSERT, Row Level Security, and Big Data 1)UPSERTUPS ...
- JAVA的面向对象编程
JAVA的面向对象编程 面向对象主要针对面向过程. 面向过程的基本单元是函数. 什么是对象:EVERYTHING IS OBJECT(万物皆对象) 全部的事物都有两个方面: 有什么(属性):用来描写叙 ...
- centos/7/isos/x86_64 下载
为了节约有限的可用带宽. 不从mirror.centos.org下载iso映像 以下镜子应该可用的ISO映像: http://mirrors.aliyun.com/centos/7/isos/x86_ ...