我的spring是3.2,mybatis是3.4

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

  1. //spring mvc core
  2. springMVC\spring-web-3.2..RELEASE.jar
  3. springMVC\spring-webmvc-3.2..RELEASE.jar
  4.  
  5. //spring core
  6. spring3.2core\commons-logging-1.2.jar
  7. spring3.2core\spring-beans-3.2..RELEASE.jar
  8. spring3.2core\spring-context-3.2..RELEASE.jar
  9. spring3.2core\spring-core-3.2..RELEASE.jar
  10. spring3.2core\spring-expression-3.2..RELEASE.jar //spring自己的表达式语言,如果不用可以不添加
  11.  
  12. //mybatis core
  13. mybatis3.4core\asm-5.2.jar
  14. mybatis3.4core\cglib-3.2..jar
  15. mybatis3.4core\commons-logging-1.2.jar
  16. mybatis3.4core\log4j-1.2..jar
  17. mybatis3.4core\mybatis-3.4..jar
  18.  
  19. //DBconnector
  20. MySQLConnector\c3p0-0.9.1.2.jar
  21. MySQLConnector\mysql-connector-java-5.1.-bin.jar
  22.  
  23. //translation
  24. springTx\spring-jdbc-3.2..RELEASE.jar
  25. springTx\spring-tx-3.2..RELEASE.jar
  26.  
  27. //AOP
  28. springAOP\aopalliance.jar
  29. springAOP\aspectjrt.jar
  30. springAOP\aspectjweaver.jar
  31. 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

  1. CREATE TABLE t_student(
  2. id INT PRIMARY KEY AUTO_INCREMENT,
  3. NAME VARCHAR() NOT NULL,
  4. age INT());

3 创建实体类Student

  1. package com.huitong.entity;
  2.  
  3. public class Student {
  4.  
  5. private Integer sid;
  6. private String sname;
  7. private Integer sage;
  8.  
  9. public Integer getSid() {
  10. return sid;
  11. }
  12. public void setSid(Integer sid) {
  13. this.sid = sid;
  14. }
  15. public String getSname() {
  16. return sname;
  17. }
  18. public void setSname(String sname) {
  19. this.sname = sname;
  20. }
  21. public Integer getSage() {
  22. return sage;
  23. }
  24. public void setSage(Integer sage) {
  25. this.sage = sage;
  26. }
  27.  
  28. }

4配置实体类的映射文件StudentMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.huitong.entity.Student">
  6. <resultMap type="com.huitong.entity.Student" id="studentMap">
  7. <id column="id" property="sid"/>
  8. <result column="name" property="sname"/>
  9. <result column="age" property="sage"/>
  10.  
  11. </resultMap>
  12.  
  13. <insert id="add" parameterType="com.huitong.entity.Student">
  14. INSERT INTO t_student(NAME, age) VALUES(#{sname},#{sage})
  15. </insert>
  16.  
  17. </mapper>

5 Student的 dao/service/action

  1. //StudentDao
  2. package com.huitong.dao;
  3.  
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6.  
  7. import com.huitong.entity.Student;
  8.  
  9. public class StudentDao {
  10.  
  11. private SqlSessionFactory sqlSessionFactory;
  12. public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
  13. this.sqlSessionFactory = sqlSessionFactory;
  14. }
  15.  
  16. public void add(Student stu) throws Exception{
  17. SqlSession sqlSession = sqlSessionFactory.openSession();
  18. sqlSession.insert(Student.class.getName() + ".add", stu);
  19. sqlSession.close();
  20. }
  21.  
  22. }
  23.  
  24. //StudentService
  25. package com.huitong.service;
  26.  
  27. import com.huitong.dao.StudentDao;
  28. import com.huitong.entity.Student;
  29.  
  30. public class StudentService {
  31.  
  32. private StudentDao studentDao;
  33. public void setStudentDao(StudentDao studentDao) {
  34. this.studentDao = studentDao;
  35. }
  36.  
  37. public void add(Student stu) throws Exception{
  38. studentDao.add(stu);
  39. }
  40.  
  41. }
  42.  
  43. //StudentAction
  44. package com.huitong.action;
  45.  
  46. import javax.annotation.Resource;
  47.  
  48. import org.springframework.stereotype.Controller;
  49. import org.springframework.ui.Model;
  50. import org.springframework.web.bind.annotation.RequestMapping;
  51.  
  52. import com.huitong.entity.Student;
  53. import com.huitong.service.StudentService;
  54.  
  55. @Controller
  56. @RequestMapping(value="/student")
  57. public class StudentAction {
  58.  
  59. private StudentService StudentService;
  60. @Resource(name="studentService")
  61. public void setStudentService(StudentService studentService) {
  62. StudentService = studentService;
  63. }
  64.  
  65. @RequestMapping(value="/register")
  66. public String register(Student stu, Model model) throws Exception{
  67. StudentService.add(stu);
  68. model.addAttribute("student", stu);
  69.  
  70. return "success";
  71. }
  72. }

6 mybatis的配置文件mybatis.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5.  
  6. <configuration>
  7. <mappers>
  8. <mapper resource="com/huitong/entity/StudentMapper.xml"/>
  9. </mappers>
  10.  
  11. </configuration>

7 spring/spring mvc配置到一个文件中spring.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:mvc="http://www.springframework.org/schema/mvc"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  16. http://www.springframework.org/schema/aop
  17. http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  18. http://www.springframework.org/schema/mvc
  19. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
  20.  
  21. <!-- dataSource -->
  22. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  23. <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  24. <property name="jdbcUrl" value="jdbc:mysql:///day17?useSSL=true"></property>
  25. <property name="user" value="root"></property>
  26. <property name="password" value=""></property>
  27. <property name="initialPoolSize" value=""></property>
  28. <property name="maxPoolSize" value=""></property>
  29. <property name="maxStatements" value=""></property>
  30. <property name="acquireIncrement" value=""></property>
  31.  
  32. </bean>
  33.  
  34. <!-- sessionFactory:datasource/xml配置文件 -->
  35. <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  36. <property name="dataSource" ref="dataSource"></property>
  37. <property name="configLocation" value="classpath:mybatis.xml"></property>
  38. </bean>
  39.  
  40. <!-- dao/service/action -->
  41. <bean id="studentDao" class="com.huitong.dao.StudentDao">
  42. <property name="sqlSessionFactory" ref="sessionFactory"></property>
  43. </bean>
  44.  
  45. <bean id="studentService" class="com.huitong.service.StudentService">
  46. <property name="studentDao" ref="studentDao"></property>
  47. </bean>
  48.  
  49. <!-- <bean id="studentAction" class="com.huitong.action.StudentAction">
  50. <property name="studentService" ref="studentService"></property>
  51. </bean> -->
  52. <!-- 使用扫描方式 -->
  53. <context:component-scan base-package="com.huitong.action"></context:component-scan>
  54.  
  55. <!-- transaction -->
  56. <!-- 4.1 txManager -->
  57. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  58. <property name="dataSource" ref="dataSource"></property>
  59. </bean>
  60.  
  61. <!-- 4.2 txAdvice -->
  62. <tx:advice id="txAdvice" transaction-manager="txManager">
  63. <tx:attributes>
  64. <tx:method name="get*" read-only="true"/>
  65. <tx:method name="query*" read-only="true"/>
  66. <tx:method name="*"/>
  67. </tx:attributes>
  68. </tx:advice>
  69.  
  70. <!-- 4.3 AOP -->
  71. <aop:config>
  72. <aop:pointcut expression="execution(* com.huitong.service.*.*(..))" id="pt"/>
  73. <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
  74. </aop:config>
  75.  
  76. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
  77. <property name="prefix" value="/jsp/"></property>
  78. <property name="suffix" value=".jsp"></property>
  79. </bean>
  80.  
  81. </beans>

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

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

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

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3.  
  4. <servlet>
  5. <servlet-name>DispatcherServlet</servlet-name>
  6. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  7. <init-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:spring.xml</param-value>
  10. </init-param>
  11. </servlet>
  12.  
  13. <servlet-mapping>
  14. <servlet-name>DispatcherServlet</servlet-name>
  15. <url-pattern>*.action</url-pattern>
  16. </servlet-mapping>
  17.  
  18. <filter>
  19. <filter-name>CharacterEncodingFilter</filter-name>
  20. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  21. <init-param>
  22. <param-name>encoding</param-name>
  23. <param-value>utf-</param-value>
  24. </init-param>
  25. </filter>
  26.  
  27. <filter-mapping>
  28. <filter-name>CharacterEncodingFilter</filter-name>
  29. <url-pattern>/*</url-pattern>
  30. </filter-mapping>
  31.  
  32. </web-app>

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

  1. SSM(Spring+SpringMVC+MyBatis)框架整合开发流程

    回忆了 Spring.SpringMVC.MyBatis 框架整合,完善一个小demo,包括基本的增删改查功能. 开发环境 IDEA MySQL 5.7 Tomcat 9 Maven 3.2.5 需要 ...

  2. SSM整合开发——基于SSM的OA系统

    一.课程介绍 链接: https://pan.baidu.com/s/18B-lWfOUnKZPvuVEHY_NmQ 提取码: ky7t 复制这段内容后打开百度网盘手机App,操作更方便哦 需要 to ...

  3. 开发步骤Dubbo、spring mvc、springboot、SSM整合开发步骤

    一.Dubbo开发步骤: 链接:https://pan.baidu.com/s/1pMPO1kf 密码:9zaa 第一: 1.创建consumer工程2.在pom.xml文件下添加配置3.添加appl ...

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

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

    ssm整合 一.导入相关包 二.开发流程 1.写entity package com.eggtwo.euq.entity; import java.io.Serializable; import ja ...

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

    Java EE互联网轻量级框架整合开发 第1部分 入门和技术基础 第1章 认识SSM框架和Redis 2 1.1 Spring框架 2 1.2 MyBatis简介 6 1.3 Spring MVC简介 ...

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

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

  8. 本人亲测-SSM整合后的基础包(供新手学习使用,可在本基础上进行二次开发)

    本案例是在eclipse上进行开发的,解压后直接添加到eclipse即可.还需要自己配置maven环境.链接:https://pan.baidu.com/s/1siuvhCJASuZG_jqY5utP ...

  9. ssm整合说明与模板-Spring Spring MVC Mybatis整合开发

    ssm整合说明 spring+spring mvc+mybatis 说明 源码下载 由于之前存在ssh框架,spring+struts+hibernate,其中spring负责aop与ioc,所以一般 ...

随机推荐

  1. 【转】Geary's C

    李旭,Matlab: Geary's C 原文地址 Introduction Geary's C is a measure of spatial autocorrelation or an attem ...

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

  3. cmd复制粘贴

    右击菜单栏,选择“快速编辑模式” 复制:选择文本后按回车,然后就可以去其他地方粘贴了 粘贴:右击鼠标就可以粘贴内容 简单到都不好意思发布出来了....

  4. Sqlserver数据库还原.bak文件失败的两个问题

    一.SQL Server数据库备份还原后,在数据库名称后会出现“受限制访问”字样      解决方案:将数据库限制访问改为:SINGLE_USER 数据库-->属性-->选项-->状 ...

  5. ylbtech-LanguageSamples-NamedAndOptional(命名和可选参数)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-NamedAndOptional(命名和可选参数) 1.A,示例(Sample) 返回顶 ...

  6. 判断客户端是PC还是手持设备的JS代码【转】

    1.第一种: 复制代码代码如下: function IsPC() {    var userAgentInfo = navigator.userAgent;    var Agents = [&quo ...

  7. Linux pci驱动源码

    #include <linux/kernel.h>#include <linux/errno.h>#include <linux/module.h>#include ...

  8. PostgreSQL 9.5,带来 UPSERT 等新特性

    PostgreSQL 9.5于2016年1月7日正式发布,此版本主要带来了以下几个方面的特性: UPSERT, Row Level Security, and Big Data 1)UPSERTUPS ...

  9. JAVA的面向对象编程

    JAVA的面向对象编程 面向对象主要针对面向过程. 面向过程的基本单元是函数. 什么是对象:EVERYTHING IS OBJECT(万物皆对象) 全部的事物都有两个方面: 有什么(属性):用来描写叙 ...

  10. centos/7/isos/x86_64 下载

    为了节约有限的可用带宽. 不从mirror.centos.org下载iso映像 以下镜子应该可用的ISO映像: http://mirrors.aliyun.com/centos/7/isos/x86_ ...