SSM框架整合(Spring+SpringMVC+MyBatis+Oracle)

SSM框架整合(Spring+SpringMVC+MyBatis+Oracle)

1.开发环境搭建以及创建Maven Web项目

     参看之前的博文:http://www.cnblogs.com/cainiaomahua/p/6306476.html

2.SSM整合

     这次整合有2个配置文件,分别是spring-mybatis.xml,包含spring和mybatis的配置文件,还有个是spring-mvc的配置文件,此外有2个资源文件:jdbc.propertis和log4j.properties。

     完整目录结构如下:

     SSM框架整合(Spring+SpringMVC+MyBatis+Oracle)_第1张图片

     使用框架的版本:

          Spring 4.0.2 RELEASE

          Spring MVC 4.0.2 RELEASE

          MyBatis 3.2.6

    2.1  Maven引入需要的JAR包

           在pom.xml中引入jar包(此处引入jar包,可以复制代码,也可以使用视图的方式,参考此链接:http://www.360doc.com/content/14/0517/18/9560593_378558295.shtml)

复制代码
  1 
  3     4.0.0
  4     org.storm
  5     storm
  6     0.0.1-SNAPSHOT
  7     storm
  8     http://maven.apache.org
  9 
 10     
 11         
 12         4.0.2.RELEASE
 13         
 14         3.2.6
 15         
 16         1.7.7
 17         1.2.17
 18     
 19 
 20     
 21         
 22             junit
 23             junit
 24             4.11
 25             
 26             test
 27         
 28 
 29         
 30         
 31             org.springframework
 32             spring-core
 33             4.0.2.RELEASE
 34         
 35         
 36             org.springframework
 37             spring-web
 38             4.0.2.RELEASE
 39         
 40         
 41             org.springframework
 42             spring-oxm
 43             4.0.2.RELEASE
 44         
 45         
 46             org.springframework
 47             spring-tx
 48             4.0.2.RELEASE
 49         
 50         
 51             org.springframework
 52             spring-jdbc
 53             4.0.2.RELEASE
 54         
 55         
 56             org.springframework
 57             spring-webmvc
 58             4.0.2.RELEASE
 59         
 60         
 61             org.springframework
 62             spring-aop
 63             4.0.2.RELEASE
 64         
 65         
 66             org.springframework
 67             spring-context-support
 68             4.0.2.RELEASE
 69         
 70         
 71             org.springframework
 72             spring-test
 73             4.0.2.RELEASE
 74         
 75 
 76         
 77         
 78             org.mybatis
 79             mybatis
 80             3.2.6
 81         
 82 
 83         
 84         
 85             org.mybatis
 86             mybatis-spring
 87             1.2.2
 88         
 89 
 90         
 91         
 92             javax
 93             javaee-api
 94             7.0
 95         
 96 
 97         
 98         
 99             com.oracle
100             ojdbc
101             6
102         
103 
104         
105         
106             commons-dbcp
107             commons-dbcp
108             1.2.2
109         
110 
111         
112         
113             jstl
114             jstl
115             1.2
116         
117 
118         
119         
120         
121             log4j
122             log4j
123             1.2.17
124         
125 
126         
127         
128             com.alibaba
129             fastjson
130             1.1.41
131         
132         
133             org.slf4j
134             slf4j-api
135             1.7.7
136         
137         
138             org.slf4j
139             slf4j-log4j12
140             1.7.7
141         
142         
143         
144         
145             org.codehaus.jackson
146             jackson-mapper-asl
147             1.9.13
148         
149 
150         
151         
152             commons-fileupload
153             commons-fileupload
154             1.3.1
155         
156         
157             commons-io
158             commons-io
159             2.4
160         
161         
162             commons-codec
163             commons-codec
164             1.9
165         
166     
167     
168         storm
169         
170             
171                 org.eclipse.jetty
172                 jetty-maven-plugin
173                 9.2.8.v20150217
174                 
175                     
176                         8090
177                     
178                     shutdown
179                     9966
180                 
181             
182         
183     
184     war
185 
复制代码

   2.2   整合SpringMVC

   2.2.1   配置spring-mvc.xml

   配置里面的注释也很详细,主要是自动扫描控制器,视图模式,注解的启动这三个。

复制代码
 1 
 2 
13     
14     
15     
16     
17     
18     
19     
20     
22         
23             
24                 text/html;charset=UTF-8
25             
26         
27     
28 
29     
30     
32         
33             
34                 
35                 
36             
37         
38     
39 
40     
41     
43         
44         
45         
46         
47         
48         
49         
50         
51     
52 
53     
55     
57         
58         
59             
60                 
61                 
62                 
63                 
64             
65         
66         
67             
68                 
69                 
71             
72         
73         
74     
75     
76     
78         
79         
80         
81     
82 
复制代码

   2.2.2   配置web.xml文件

    配置的spring-mvc的Servlet就是为了完成SpringMVC+MAVEN的整合。

    web.xml  

复制代码
 1 
 2 
 6     Archetype Created Web Application
 7     
 8     
12     
13     
14         encodingFilter
15         org.springframework.web.filter.CharacterEncodingFilter
16         true
17         
18             encoding
19             UTF-8
20         
21     
22     
23         encodingFilter
24         /*
25     
26     
27     
30     
31     
34 
35     
36     
37         SpringMVC
38         org.springframework.web.servlet.DispatcherServlet
39         
40         
41             contextConfigLocation
42             classpath:spring-mvc.xml
43         
44         1
45         true
46     
47     
48         SpringMVC
49         
50         /
51     
52     
53         /index.jsp
54     
55 
复制代码

   2.2.3   Log4j的配置

       为了方便调试,一般都会使用日志来输出信息,Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。

      Log4j的配置很简单,而且也是通用的,下面给出一个基本的配置,换到其他项目中也无需做多大的调整。

      log4j.properties

复制代码
 1 #定义LOG输出级别
 2 log4j.rootLogger=INFO,Console,File
 3 #定义日志输出目的地为控制台
 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender
 5 log4j.appender.Console.Target=System.out
 6 #可以灵活地指定日志输出格式,下面一行是指定具体的格式
 7 log4j.appender.Console.layout=org.apache.log4j.PatternLayout
 8 log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
 9 
10 #文件大小达到指定尺寸的时候产生一个新的文件
11 log4j.appender.File=org.apache.log4j.RollingFileAppender
12 #指定输出目录
13 log4j.appender.File.File=logs/ssm.log
14 #定义文件最大大小
15 log4j.appender.File.MaxFileSize=10MB
16 #输出所有日志,如果换成DEBUG表示输出DEBUG以上级别日志
17 log4j.appender.File.Threshold=ALL
18 log4j.appender.File.layout=org.apache.log4j.PatternLayout  
19 log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n 
复制代码

     2.2.4  使用Jetty测试

     Student类对应于数据库中表的属性

复制代码
 1 package com.cn.hnust.pojo;
 2 
 3 public class Student {
 4     private Integer id;
 5 
 6     private String stu_name;
 7 
 8     private Integer gender;
 9 
10     private Integer age;
11 
12     private String address;
13 
14     public Integer getId() {
15         return id;
16     }
17 
18     public void setId(Integer id) {
19         this.id = id;
20     }
21 
22     public String getStu_name() {
23         return stu_name;
24     }
25 
26     public void setStu_name(String stu_name) {
27         this.stu_name = stu_name;
28     }
29 
30     public Integer getGender() {
31         return gender;
32     }
33 
34     public void setGender(Integer gender) {
35         this.gender = gender;
36     }
37 
38     public Integer getAge() {
39         return age;
40     }
41 
42     public void setAge(Integer age) {
43         this.age = age;
44     }
45 
46     public String getAddress() {
47         return address;
48     }
49 
50     public void setAddress(String address) {
51         this.address = address;
52     }
53 
54     @Override
55     public String toString() {
56         return "Student [id=" + id + ", stu_name=" + stu_name + ", gender="
57                 + gender + ", age=" + age + ", address=" + address + "]";
58     }
59 }
复制代码

 

    控制器类

复制代码
 1 package com.cn.hnust.controller;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.Map;
 6 
 7 import javax.annotation.Resource;
 8 import javax.servlet.http.HttpServletRequest;
 9 
10 import org.apache.commons.io.FileUtils;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.http.HttpStatus;
14 import org.springframework.http.ResponseEntity;
15 import org.springframework.stereotype.Controller;
16 import org.springframework.ui.Model;
17 import org.springframework.web.bind.annotation.PathVariable;
18 import org.springframework.web.bind.annotation.RequestMapping;
19 import org.springframework.web.bind.annotation.RequestMethod;
20 import org.springframework.web.bind.annotation.RequestParam;
21 import org.springframework.web.bind.annotation.ResponseBody;
22 import org.springframework.web.multipart.MultipartFile;
23 
24 import com.cn.hnust.pojo.Student;
25 import com.cn.hnust.service.IStudentService;
26 
27 @Controller
28 @RequestMapping("/student")
29 public class StudentController {
30     private static Logger log = LoggerFactory
31             .getLogger(StudentController.class);
32 
33     @Resource
34     private IStudentService studentService;
35 
36     // /student/test?id=1
37     @RequestMapping(value = "/test", method = RequestMethod.GET)
38     public String test(HttpServletRequest request, Model model) {
39         int studentId = Integer.parseInt(request.getParameter("id"));
40         System.out.println("test studentId:" + studentId);
41         Student student = null;
42         if (studentId == 1) {
43             student = new Student();
44             student.setAge(19);
45             student.setId(1);
46             student.setGender(1);
47             student.setAddress("北京七里省际大厦3栋4402");
48             student.setStu_name("江泽");
49         }
50 
51         log.debug(student.toString());
52         model.addAttribute("student", student);
53         return "index";
54     }
55 }
复制代码

    运行测试

    SSM框架整合(Spring+SpringMVC+MyBatis+Oracle)_第2张图片

    SSM框架整合(Spring+SpringMVC+MyBatis+Oracle)_第3张图片

    控制台没有报错

    SSM框架整合(Spring+SpringMVC+MyBatis+Oracle)_第4张图片

     在浏览器中输入:http://localhost:8090/student/test?id=1      (端口号根据你的设置有关,自己调查)

     到此 SpringMVC+Maven 整合完毕

    2.3 Spring与MyBatis的整合

           取消2.2.2 web.xml中注释的代码,下面是去除后的代码

复制代码
 1 
 2 
 6     Archetype Created Web Application
 7     
 8     
 9         contextConfigLocation
10         classpath:spring-mybatis.xml
11     
12     
13     
14         encodingFilter
15         org.springframework.web.filter.CharacterEncodingFilter
16         true
17         
18             encoding
19             UTF-8
20         
21     
22     
23         encodingFilter
24         /*
25     
26     
27     
28         org.springframework.web.context.ContextLoaderListener
29     
30     
31     
32         org.springframework.web.util.IntrospectorCleanupListener
33     
34 
35     
36     
37         SpringMVC
38         org.springframework.web.servlet.DispatcherServlet
39         
40         
41             contextConfigLocation
42             classpath:spring-mvc.xml
43         
44         1
45         true
46     
47     
48         SpringMVC
49         
50         /
51     
52     
53         /index.jsp
54     
55 
复制代码

     2.3.1、建立JDBC属性文件

       jdbc.properties(文件编码修改为utf-8)   根据自己数据库配置url  username  password

复制代码
 1 #连接数据库
 2 #jdbc.driver=net.sf.log4jdbc.DriverSpy
 3 #jdbc.url=jdbc:log4jdbc:oracle:thin:@192.168.0.125:1521:xe
 4 jdbc.driver=oracle.jdbc.OracleDriver
 5 jdbc.url=jdbc:oracle:thin:@192.168.0.125:1521:xe
 6 jdbc.username=orcl
 7 jdbc.password=orcl
 8 
 9 #dbcp settings
10 dbcp.maxIdle=5
11 dbcp.maxActive=50
复制代码

    2.3.2、建立spring-mybatis.xml配置文件

    这个文件就是用来完成spring和mybatis的整合的。主要的就是自动扫描,自动注入,配置数据库。注释也很详细,大家看看就明白了。

    spring-mybatis.xml

复制代码
 1 
 2 
13     
14     
15     
16     
17     
20     
21     
22     
24         
25     
26 
27     
28     
30         
31         
32         
33         
34         
35 
36         
37         
38         
39         
40         
41         
42         
43     
44 
45     
46     
48         
49         
50         
51     
52     
53     
54         
55         
56     
57     
58       
59       
61           
62      
63 
复制代码

    2.4   JUnit测试

    创建实体类、MyBatis映射文件以及DAO接口

    DAO接口类

复制代码
 1 package com.cn.hnust.dao;
 2 
 3 import com.cn.hnust.pojo.Student;
 4 
 5 public interface IStudentDao {
 6     int deleteByPrimaryKey(Integer id);
 7     
 8     int insert(Student record);
 9     
10     int insertSelective(Student record);
11     
12     Student selectByPrimaryKey(Integer id);
13     
14     int updateByPrimaryKeySelective(Student record);
15     
16     int updateByPrimaryKey(Student record);
17 }
复制代码

    实现增删改查的xml配置文件

复制代码
 1 
 2 
 5 
 6     
 7         
 8         
 9         
10         
11         
12     
13     
14         id,stu_name,age,gender,address
15     
16     
23     
24         delete from
25         student
26         where id = #{id,jdbcType=INTEGER}
27     
28     
29         insert into
30         student(id,stu_name,age,gender,address)
31         values
32         (#{id,jdbcType=INTEGER},#{stu_name,jdbcType=VARCHAR},#{age,jdbcType=INTEGER},#{gender,jdbcType=INTEGER},#{address,jdbcType=VARCHAR})
33     
34     
35         insert into student
36         
37             
38                 id,
39             
40             
41                 stu_name,
42             
43             
44                 age,
45             
46             
47                 gender,
48             
49             
50                 address,
51             
52         
53         
54             
55                 #{id,jdbcType=INTEGER},
56             
57             
58                 #{stu_name,jdbcType=VARCHAR},
59             
60             
61                 #{age,jdbcType=INTEGER},
62             
63             
64                 #{gender,jdbcType=INTEGER},
65             
66             
67                 #{address,jdbcType=VARCHAR},
68             
69         
70     
71     
72         update student
73         
74             
75                 stu_name = #{stu_name,jdbcType=VARCHAR},
76             
77             
78                 age = #{age,jdbcType=INTEGER},
79             
80             
81                 gender = #{gender,jdbcType=INTEGER},
82             
83             
84                 address = #{address,jdbcType=VARCHAR},
85             
86         
87         where id = #{id,jdbcType=INTEGER}
88     
89     
90         update student
91         set stu_name = #{stu_name,jdbcType=VARCHAR},
92             age = #{age,jdbcType=INTEGER},
93             gender = #{gender,jdbcType=INTEGER},
94             address = #{address,jdbcType=VARCHAR}
95         where id = #{id,jdbcType=INTEGER}
96     
97 
复制代码

    建立Service接口和实现类

复制代码
1 package com.cn.hnust.service;
2 
3 import com.cn.hnust.pojo.Student;
4 
5 public interface IStudentService {
6     public Student getStudentById(int studentId);
7 }
复制代码

    

    

复制代码
 1 package com.cn.hnust.service.impl;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.springframework.stereotype.Service;
 6 
 7 import com.cn.hnust.dao.IStudentDao;
 8 import com.cn.hnust.pojo.Student;
 9 import com.cn.hnust.service.IStudentService;
10 
11 @Service("studentService")
12 public class StudentServiceImpl implements IStudentService {
13 
14     @Resource
15     private IStudentDao studentDao;
16 
17     @Override
18     public Student getStudentById(int studentId) {
19         return this.studentDao.selectByPrimaryKey(studentId);
20     }
21 
22 }
复制代码

    建立UserController类

复制代码
  1 package com.cn.hnust.controller;
  2 
  3 import java.io.File;
  4 import java.io.IOException;
  5 import java.util.Map;
  6 
  7 import javax.annotation.Resource;
  8 import javax.servlet.http.HttpServletRequest;
  9 
 10 import org.apache.commons.io.FileUtils;
 11 import org.slf4j.Logger;
 12 import org.slf4j.LoggerFactory;
 13 import org.springframework.http.HttpStatus;
 14 import org.springframework.http.ResponseEntity;
 15 import org.springframework.stereotype.Controller;
 16 import org.springframework.ui.Model;
 17 import org.springframework.web.bind.annotation.PathVariable;
 18 import org.springframework.web.bind.annotation.RequestMapping;
 19 import org.springframework.web.bind.annotation.RequestMethod;
 20 import org.springframework.web.bind.annotation.RequestParam;
 21 import org.springframework.web.bind.annotation.ResponseBody;
 22 import org.springframework.web.multipart.MultipartFile;
 23 
 24 import com.cn.hnust.pojo.Student;
 25 import com.cn.hnust.service.IStudentService;
 26 
 27 @Controller
 28 @RequestMapping("/student")
 29 public class StudentController {
 30     private static Logger log = LoggerFactory
 31             .getLogger(StudentController.class);
 32 
 33     @Resource
 34     private IStudentService studentService;
 35 
 36     // /student/test?id=1
 37     @RequestMapping(value = "/test", method = RequestMethod.GET)
 38     public String test(HttpServletRequest request, Model model) {
 39         int studentId = Integer.parseInt(request.getParameter("id"));
 40         System.out.println("test studentId:" + studentId);
 41         Student student = null;
 42         if (studentId == 1) {
 43             student = new Student();
 44             student.setAge(19);
 45             student.setId(1);
 46             student.setGender(1);
 47             student.setAddress("北京七里省际大厦3栋4402");
 48             student.setStu_name("江泽");
 49         }
 50 
 51         log.debug(student.toString());
 52         model.addAttribute("student", student);
 53         return "index";
 54     }
 55 
 56     // /student/showStudent?id=1
 57     @RequestMapping(value = "/showStudent", method = RequestMethod.GET)
 58     public String toIndex(HttpServletRequest request, Model model) {
 59         int studentId = Integer.parseInt(request.getParameter("id"));
 60         System.out.println("toIndex studentId:" + studentId);
 61         Student student = this.studentService.getStudentById(studentId);
 62         log.debug(student.toString());
 63         model.addAttribute("student", student);
 64         return "showStudent";
 65     }
 66 
 67     // /student/showStudent2?id=1
 68     @RequestMapping(value = "/showStudent2", method = RequestMethod.GET)
 69     public String toIndex2(@RequestParam("id") String id, Model model) {
 70         int studentId = Integer.parseInt(id);
 71         System.out.println("toIndex2 studentId:" + studentId);
 72         Student student = this.studentService.getStudentById(studentId);
 73         log.debug(student.toString());
 74         model.addAttribute("student", student);
 75         return "showStudent";
 76     }
 77 
 78     // /student/showStudent3/{id}
 79     @RequestMapping(value = "/showStudent3/{id}", method = RequestMethod.GET)
 80     public String toIndex3(@RequestParam("id") String id,
 81             Map model) {
 82         int studentId = Integer.parseInt(id);
 83         System.out.println("toIndex3 studentId:" + studentId);
 84         Student student = this.studentService.getStudentById(studentId);
 85         log.debug(student.toString());
 86         model.put("student", student);
 87         return "showStudent";
 88     }
 89 
 90     // /student{id}
 91     @RequestMapping(value = "/{id}", method = RequestMethod.GET)
 92     public @ResponseBody
 93     Student getStudentInJson(@PathVariable String id, Map model) {
 94         int studentId = Integer.parseInt(id);
 95         System.out.println("getStudentInJson studentId:" + studentId);
 96         Student student = this.studentService.getStudentById(studentId);
 97         log.info(student.toString());
 98         return student;
 99     }
100 
101     // /student/{id}
102     @RequestMapping(value = "/jsontype/{id}", method = RequestMethod.GET)
103     public ResponseEntity getStudentInJson2(@PathVariable String id,
104             Map model) {
105         int studentId = Integer.parseInt(id);
106         System.out.println("getStudentInJson2 studentId:" + studentId);
107         Student student = this.studentService.getStudentById(studentId);
108         log.info(student.toString());
109         return new ResponseEntity(student, HttpStatus.OK);
110     }
111 
112     // 文件上传
113     @RequestMapping(value = "/upload")
114     public String showUploadPage() {
115         return "student_admin/file";
116     }
117 
118     @RequestMapping(value = "/doUpload", method = RequestMethod.GET)
119     public String doUploadFile(@RequestParam("file") MultipartFile file)
120             throws IOException {
121         if (!file.isEmpty()) {
122             log.info("Process file:{}", file.getOriginalFilename());
123         }
124         FileUtils.copyInputStreamToFile(file.getInputStream(), new File("F:\\",
125                 System.currentTimeMillis() + file.getOriginalFilename()));
126         return "sucess";
127     }
128 }
复制代码

    新建jsp页面

    file.jsp

复制代码
 1 <%@ page language="java" contentType="text/html;charset=utf-8"
 2     pageEncoding="utf-8" %>
 3 
 4 
 5 
 6     
 7     Insert title here
 8 
 9 
10     

上传文件

11
12 13 14
15 16
复制代码

    index.jsp

复制代码
 1 <%@ page language="java" import="java.util.*"
 2     pageEncoding="utf-8" %>
 3 
 4 
 5 
 6 

Hello zou

7
8 跳转 9 10
复制代码

    showUser.jsp

复制代码
 1 <%@ page language="java" import="java.util.*"
 2     pageEncoding="utf-8" %>
 3 
 4 
 5 
 6     
 7     Testing
 8 
 9 
10     ${student.stu_name}
11 
12 
复制代码

    success.jsp

复制代码
 1 <%@ page language="java" contentType="text/html;charset=utf-8"
 2     pageEncoding="utf-8" %>
 3 
 4 
 5 
 6     
 7     success
 8 
 9 
10     

success

11 12
复制代码

    部署项目

    输入地址:http://localhost:8090/student/jsontype/2

    

 

转载于:https://www.cnblogs.com/weixupeng/p/8540563.html

你可能感兴趣的:(SSM框架整合(Spring+SpringMVC+MyBatis+Oracle))