简单的搭建一个SSM项目

一直以来都想去写一写博文,记录一下自己的成长过程,因为拖延症一直都没有去写,而以往学过的知识,不用的就慢慢遗忘了.

我是一个菜鸟,但有一个大牛梦,若是有不妥当之处,还望大家不吝赐教!

1.添加jar

 1 <dependency>
 2         <groupId>org.springframework</groupId>
 3         <artifactId>spring-context</artifactId>
 4         <version>5.0.6.RELEASE</version>
 5     </dependency>
 6     
 7     <dependency>
 8         <groupId>org.springframework</groupId>
 9         <artifactId>spring-core</artifactId>
10         <version>5.0.6.RELEASE</version>
11     </dependency>
12     
13     <dependency>
14         <groupId>org.springframework</groupId>
15         <artifactId>spring-beans</artifactId>
16         <version>5.0.6.RELEASE</version>
17     </dependency>
18     
19     <dependency>
20         <groupId>org.springframework</groupId>
21         <artifactId>spring-web</artifactId>
22         <version>5.0.6.RELEASE</version>
23     </dependency>
24     
25     <dependency>
26         <groupId>org.springframework</groupId>
27         <artifactId>spring-webmvc</artifactId>
28         <version>5.0.6.RELEASE</version>
29     </dependency>
30     
31     <dependency>
32         <groupId>org.springframework</groupId>
33         <artifactId>spring-aop</artifactId>
34         <version>5.0.6.RELEASE</version>
35     </dependency>
36     
37     <dependency>
38         <groupId>org.springframework</groupId>
39         <artifactId>spring-jdbc</artifactId>
40         <version>5.0.5.RELEASE</version>
41     </dependency>
42     
43     <dependency>
44         <groupId>org.springframework</groupId>
45         <artifactId>spring-tx</artifactId>
46         <version>5.0.5.RELEASE</version>
47     </dependency>
48     
49     <!-- Tomcat服务器 -->
50     <dependency>
51         <groupId>org.apache.tomcat</groupId>
52         <artifactId>tomcat-catalina</artifactId>
53         <version>8.5.2</version>
54     </dependency>
55     
56      <!-- mybatis -->
57      <dependency>
58         <groupId>org.mybatis</groupId>
59         <artifactId>mybatis</artifactId>
60         <version>3.4.5</version>
61     </dependency>
62     
63     <dependency>
64         <groupId>org.mybatis</groupId>
65         <artifactId>mybatis-spring</artifactId>
66         <version>1.3.1</version>
67     </dependency>
68     <!-- mysql -->
69     <dependency>
70         <groupId>mysql</groupId>
71         <artifactId>mysql-connector-java</artifactId>
72         <version>8.0.11</version>
73     </dependency>
74     <!--c3p0  -->
75     <dependency>
76         <groupId>com.mchange</groupId>
77         <artifactId>c3p0</artifactId>
78         <version>0.9.5.2</version>
79     </dependency>
80     
81     <dependency>
82         <groupId>javax.servlet</groupId>
83         <artifactId>jstl</artifactId>
84         <version>1.2</version>
85     </dependency>

2.项目包结构图

3.代码

   扩展AbstractAnnotationConfigDispatcherServletInitializer的任意类都会自动配置DisPatcher-Servlet和Spring应用上下文,Spring的应用上下文会位于程序的Servlet上下文之中

1.MyWebMVCInitializer 类

 1 package org.great.config;
 2 
 3 import javax.servlet.Filter;
 4 
 5 import org.springframework.web.filter.CharacterEncodingFilter;
 6 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
 7 
 8 public class MyWebMVCInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
 9 
10     @Override
11     protected Class<?>[] getRootConfigClasses() {
12         return new Class[] {RootConfig.class};
13     }
14 
15     @Override
16     protected Class<?>[] getServletConfigClasses() {
17         return new Class[] {WebConfig.class};
18     }
19     
20     
21     @Override
22     protected String[] getServletMappings() {
23         return new String[] {"*.do"};
24     }
25     
26     /**
27      * 编码过滤器
28      */
29     @Override
30     protected Filter[] getServletFilters() {
31         return new Filter[] {new CharacterEncodingFilter("utf-8")};
32     }
33 }

2.WebConfig类

 1 package org.great.config;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.ComponentScan;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.web.servlet.ViewResolver;
 7 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 8 import org.springframework.web.servlet.view.InternalResourceViewResolver;
 9 
10 @Configuration
11 @EnableWebMvc    // <mvc:annotation-driven> 
12 @ComponentScan(basePackages= {"org.great.web"})
13 public class WebConfig {
14     
15     /**
16      * 配置视图解析器
17      * @return
18      */
19     @Bean
20     public ViewResolver viewResolver() {
21         InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
22         internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
23         internalResourceViewResolver.setSuffix(".jsp");
24         return internalResourceViewResolver;
25     }
26 }

3.RootConfig类

 1 package org.great.config;
 2 
 3 import javax.sql.DataSource;
 4 
 5 import org.mybatis.spring.SqlSessionFactoryBean;
 6 import org.mybatis.spring.annotation.MapperScan;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.context.annotation.ComponentScan;
 9 import org.springframework.context.annotation.Configuration;
10 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
11 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
12 import org.springframework.transaction.annotation.EnableTransactionManagement;
13 
14 import com.mchange.v2.c3p0.ComboPooledDataSource;
15 
16 @Configuration
17 @ComponentScan(basePackages= {"org.great.service"})
18 @MapperScan(basePackages= {"org.great.mapper"})
19 @EnableTransactionManagement  //<tx:annotation-driven/>   用于启动注解 事务
20 public class RootConfig {
21     
22     
23     @Bean
24     public DataSource dataSource() throws Exception{
25         ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
26         comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
27         comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test607?useSSL=false&serverTimezone=GMT");
28         comboPooledDataSource.setUser("root");
29         comboPooledDataSource.setPassword("");
30         return comboPooledDataSource;
31     }
32     
33     @Bean
34     public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception{
35         SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
36         sqlSessionFactoryBean.setDataSource(this.dataSource());
37         PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
38         sqlSessionFactoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources("classpath:org/great/sql/*.xml"));
39         return sqlSessionFactoryBean;
40     }
41 
42     @Bean
43     public DataSourceTransactionManager dataSourceTransactionManager() throws Exception{
44         DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
45         dataSourceTransactionManager.setDataSource(this.dataSource());
46         return dataSourceTransactionManager;
47     }
48     
49     // 使用@MapperScan(basePackages= {"org.great.mapper"})代替
50     /*@Bean
51     public MapperScannerConfigurer mapperScannerConfigurer() throws Exception{
52         MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
53         mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
54         mapperScannerConfigurer.setBasePackage("org.great.mapper");
55         return mapperScannerConfigurer;
56     }*/
57 }

4.User实体类

 1 package org.great.entity;
 2 
 3 public class User {
 4     
 5     private int id;
 6     
 7     private String name;
 8     
 9     private String age;
10 
11     public int getId() {
12         return id;
13     }
14 
15     public void setId(int id) {
16         this.id = id;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public String getAge() {
28         return age;
29     }
30 
31     public void setAge(String age) {
32         this.age = age;
33     }
34 
35     @Override
36     public String toString() {
37         return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
38     }
39 
40     public User(int id, String name, String age) {
41         super();
42         this.id = id;
43         this.name = name;
44         this.age = age;
45     }
46 
47     public User() {
48         super();
49     }
50 }

5.IUserMapper接口

 1 package org.great.mapper;
 2 
 3 import java.util.List;
 4 
 5 import org.great.entity.User;
 6 import org.springframework.stereotype.Repository;
 7 
 8 @Repository
 9 public interface IUserMapper {
10     
11     public List<User> selectAllUser();
12     
13     public void insertUser(User user);
14 }

6.UserService类

 1 package org.great.service;
 2 
 3 import java.util.List;
 4 
 5 import org.great.entity.User;
 6 import org.great.mapper.IUserMapper;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.stereotype.Service;
 9 import org.springframework.transaction.annotation.Transactional;
10 
11 @Service
12 public class UserService {
13     
14     @Autowired
15     private IUserMapper iUserMapper;
16     
17     public List<User> selectAllUserService() {
18         List<User> selectAllUser = iUserMapper.selectAllUser();
19         return selectAllUser;
20     }
21     
22     @Transactional
23     public void insertUserService() {
24         User user = new User();
25         user.setName("王五");
26         user.setAge("23");
27         iUserMapper.insertUser(user);
28     }
29 }

7.UserControll类

 1 package org.great.web;
 2 
 3 import java.util.List;
 4 
 5 import org.great.entity.User;
 6 import org.great.service.UserService;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.stereotype.Controller;
 9 import org.springframework.ui.Model;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 
12 @Controller
13 public class UserController {
14     
15     @Autowired
16     private UserService userService;
17 
18     @RequestMapping("/firstView")
19     public String firstView(Model model) {
20         List<User> selectAllUserService = userService.selectAllUserService();
21         model.addAttribute("userList", selectAllUserService);
22         return "home";
23     }
24     
25     @RequestMapping("/insertUser")
26     public String insertUser(Model model) {
27         userService.insertUserService();
28         return "redirect:/firstView.do";
29     }
30 }

8.User.xml配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <!DOCTYPE mapper
 4     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 5     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 6 
 7 <mapper namespace="org.great.mapper.IUserMapper">
 8 
 9     <resultMap type="org.great.entity.User" id="UserResult">
10         <id column="id" jdbcType="INTEGER" property="id" />
11         <result column="name" jdbcType="VARCHAR" property="name" />
12         <result column="age" jdbcType="VARCHAR" property="age" />
13     </resultMap>
14     
15     <select id="selectAllUser" resultMap="UserResult">
16         select * from user
17     </select>
18     
19     <insert id="insertUser">
20         insert into user (name,age) values(#{name},#{age})
21     </insert>
22 
23 </mapper>

9.home,jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page isELIgnored="false" %>
 4 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>Insert title here</title>
10 </head>
11 <body>
12     <table>
13         <tr>
14             <td>序号</td>
15             <td>名字</td>
16             <td>年龄</td>
17         </tr>
18         <c:forEach items="${userList}" var="user" varStatus="ind">
19             <tr>
20                 <td>${ind.count}</td>
21                 <td>${user.name}</td>
22                 <td>${user.age}</td>
23             </tr>    
24         </c:forEach>
25     </table>
26 </body>
27 </html>

猜你喜欢

转载自www.cnblogs.com/ke609/p/9160453.html