转载

SSM框架--Spring+SpringMVC+Mybatis (IDEA)搭建

使用idea创建一个maven项目( 这里演示 的是 web项目)

点击 Finish

然后开始配置 pom.xml文件(添加各种依赖jar包)

先去找 spring 所需的 jar包

jar包中心仓库地址:http://mvnrepository.com/

 

 找到 Spring Web ( 因为 我这里演示的是一个web项目)

把这段 复制到 pom.xml文件中

下面是我配置完整的pom.xml文件 ( 这里 我只在dependencies的下面 添加依赖 其他的 我都没有动  )

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6 
  7   <groupId>com.oukele.demo</groupId>
  8   <artifactId>SSM_demo</artifactId>
  9   <version>1.0-SNAPSHOT</version>
 10   <packaging>war</packaging>
 11 
 12   <name>SSM_demo Maven Webapp</name>
 13   <!-- FIXME change it to the project's website -->
 14   <url>http://www.example.com</url>
 15 
 16   <properties>
 17     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 18     <maven.compiler.source>1.7</maven.compiler.source>
 19     <maven.compiler.target>1.7</maven.compiler.target>
 20   </properties>
 21 
 22   <!-- 依赖 -->
 23   <dependencies>
 24 
 25     <!-- spring 依赖包 -->
 26     <!-- spring web jar包 -->
 27     <dependency>
 28       <groupId>org.springframework</groupId>
 29       <artifactId>spring-web</artifactId>
 30       <version>5.1.0.RELEASE</version>
 31     </dependency>
 32     <!-- spring aop jar包 -->
 33     <dependency>
 34       <groupId>org.springframework</groupId>
 35       <artifactId>spring-aop</artifactId>
 36       <version>5.1.0.RELEASE</version>
 37     </dependency>
 38     <!-- spring 驱动jar包-->
 39     <dependency>
 40       <groupId>org.springframework</groupId>
 41       <artifactId>spring-jdbc</artifactId>
 42       <version>5.1.0.RELEASE</version>
 43     </dependency>
 44     <!-- spring mvc 依赖包 -->
 45     <dependency>
 46       <groupId>org.springframework</groupId>
 47       <artifactId>spring-webmvc</artifactId>
 48       <version>5.1.0.RELEASE</version>
 49     </dependency>
 50     <!-- mybatis 依赖包 -->
 51     <!--mybatis jar包 -->
 52     <dependency>
 53       <groupId>org.mybatis</groupId>
 54       <artifactId>mybatis</artifactId>
 55       <version>3.4.6</version>
 56     </dependency>
 57     <!--  mybatis spring-->
 58     <dependency>
 59       <groupId>org.mybatis</groupId>
 60       <artifactId>mybatis-spring</artifactId>
 61       <version>1.3.2</version>
 62     </dependency>
 63 
 64     <!-- 连接数据库驱动的jar包 我这里使用的是 mariadb数据库的驱动包 -->
 65     <dependency>
 66       <groupId>org.mariadb.jdbc</groupId>
 67       <artifactId>mariadb-java-client</artifactId>
 68       <version>2.3.0</version>
 69     </dependency>
 70 
 71     <!-- 连接接池 c3p0 -->
 72     <dependency>
 73       <groupId>com.mchange</groupId>
 74       <artifactId>c3p0</artifactId>
 75       <version>0.9.5.2</version>
 76     </dependency>
 77 
 78     <!-- jstl jar包 -->
 79     <dependency>
 80       <groupId>javax.servlet</groupId>
 81       <artifactId>jstl</artifactId>
 82       <version>1.2</version>
 83     </dependency>
 84     
 85     <!-- 单元测试 -->
 86     <dependency>
 87       <groupId>junit</groupId>
 88       <artifactId>junit</artifactId>
 89       <version>4.11</version>
 90       <scope>test</scope>
 91     </dependency>
 92 
 93   </dependencies>
 94 
 95 
 96   <build>
 97     <finalName>SSM_demo</finalName>
 98     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
 99       <plugins>
100         <plugin>
101           <artifactId>maven-clean-plugin</artifactId>
102           <version>3.0.0</version>
103         </plugin>
104         <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
105         <plugin>
106           <artifactId>maven-resources-plugin</artifactId>
107           <version>3.0.2</version>
108         </plugin>
109         <plugin>
110           <artifactId>maven-compiler-plugin</artifactId>
111           <version>3.7.0</version>
112         </plugin>
113         <plugin>
114           <artifactId>maven-surefire-plugin</artifactId>
115           <version>2.20.1</version>
116         </plugin>
117         <plugin>
118           <artifactId>maven-war-plugin</artifactId>
119           <version>3.2.0</version>
120         </plugin>
121         <plugin>
122           <artifactId>maven-install-plugin</artifactId>
123           <version>2.5.2</version>
124         </plugin>
125         <plugin>
126           <artifactId>maven-deploy-plugin</artifactId>
127           <version>2.8.2</version>
128         </plugin>
129       </plugins>
130     </pluginManagement>
131   </build>
132 </project>

配置完成后

等待 jar包 下载完成。

这些都是下载的jar包(这样pom.xml 文件 算是配置完成了。)

接下来开始 其他 xml文件的配置

main下面 如果 少一个 java 文件 (我们手动新建)

 

java 文件变成蓝色的就可以了。

建立项目结构

开始配置 spring xml 和 spring mvc xml

在 main 目录里 创建 一个 resources 文件,然后将它标志成 资源文件夹

在resources文件夹中 新建一个 spring_root.xml 文件

spring_root文件的完整配置信息

 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:contxt="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 6 
 7     <!-- 扫描 service 包下面使用@Component/@Service/@Repository/@Controller 注解的类 将其实例化,放入容器中   -->
 8     <contxt:component-scan base-package="com.oukele.bookshop_ssm.service" />
 9 
10     <!--创建数据源-->
11     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
12         <!-- 数据库驱动 -->
13         <property name="driverClass" value="org.mariadb.jdbc.Driver" />
14         <!-- 数据库连接 -->
15         <property name="jdbcUrl" value="jdbc:mariadb://localhost:3306/data_test" />
16         <!-- 数据库用户名 -->
17         <property name="user" value="oukele"/>
18         <!-- 数据库密码 -->
19         <property name="password" value="oukele"/>
20     </bean>
21 
22     <!-- 配置mybatis  -->
23     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
24         <!-- 数据源的实例化 -->
25         <property name="dataSource" ref="dataSource"/>
26         <!-- 映射 DML 语法 -->
27         <property name="mapperLocations" value="classpath:mapper/*.xml"/>
28     </bean>
29     <!-- 映射 数据访问层 ( dao ) 中的接口 -->
30     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
31         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
32         <property name="basePackage" value="com.oukele.bookshop_ssm.dao"/>
33     </bean>
34 
35 </beans>

spring_web文件的完整配置信息

 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:contxt="http://www.springframework.org/schema/context"
 5        xmlns:mvc="http://www.springframework.org/schema/mvc"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 7 
 8     <!-- 扫描 控制层 (web) 下面 @Controller 注解的类 将其实例化,放入容器中 -->
 9     <contxt:component-scan base-package="com.oukele.bookshop_ssm.web"/>
10     <!-- 启动 mvc 的常用注解 -->
11     <mvc:annotation-driven/>
12     <!--将所有的静态资源,交给 Servlet 处理-->
13     <mvc:default-servlet-handler/>
14 
15     <!--配置 view 解析器-->
16     <!-- 页面接受数据 -->
17     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
18         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
19         <property name="prefix" value="/WEB-INF/views/" />
20         <property name="suffix" value=".jsp" />
21     </bean>
22 
23 </beans>

 在resources 文件夹 新建一个包 ( 包名随意趣吧,有意义一些的)

然后再那个包里新建一个 xml文件 用来映射 DML语法

BookMapper.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 
 6 <!-- 映射 接口 IBookDao -->
 7 <mapper namespace="com.oukele.bookshop_ssm.dao.IBookDao">
 8 
 9     <!-- 这里的id 是 接口中的方法名  -->
10     <select id="listAll" resultType="com.oukele.bookshop_ssm.entity.Book">
11         select * from ssm_book
12     </select>
13 
14 </mapper>

 

这里xml文件差不多配置完成了。

开始写代码啦。

 

(entity)实体类。

 1 package com.nf147.bookstore_ssm.entity;
 2 
 3 public class Book {
 4     private int bookid;
 5     private String name;
 6     private int cnt;
 7 
 8     public Book() {
 9     }
10 
11     public Book(String name, int cnt) {
12         this.name = name;
13         this.cnt = cnt;
14     }
15 
16     public int getBookid() {
17         return bookid;
18     }
19 
20     public void setBookid(int bookid) {
21         this.bookid = bookid;
22     }
23 
24     public String getName() {
25         return name;
26     }
27 
28     public void setName(String name) {
29         this.name = name;
30     }
31 
32     public int getCnt() {
33         return cnt;
34     }
35 
36     public void setCnt(int cnt) {
37         this.cnt = cnt;
38     }
39 
40     @Override
41     public String toString() {
42         return "Book{" +
43                 "bookid=" + bookid +
44                 ", name='" + name + '\'' +
45                 ", cnt=" + cnt +
46                 '}';
47     }
48 }

(dao)数据访问层

 1 package com.oukele.bookshop_ssm.dao;
 2 
 3 import com.oukele.bookshop_ssm.entity.Book;
 4 import org.springframework.stereotype.Repository;
 5 import java.util.List;
 6 
 7 @Repository
 8 public interface IBookDao {
 9     List<Book> listAll();
10 }

 (service)服务层

1 package com.oukele.bookshop_ssm.service;
2 
3 import com.oukele.bookshop_ssm.entity.Book;
4 
5 import java.util.List;
6 
7 public interface BookService {
8     List<Book> listAll();
9 }
 1 package com.oukele.bookshop_ssm.service;
 2 
 3 import com.oukele.bookshop_ssm.dao.IBookDao;
 4 import com.oukele.bookshop_ssm.entity.Book;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 import java.util.List;
 9 
10 @Service
11 public class BookServiceTemp implements BookService {
12 
13 //    使用 dao 中的接口
14     @Autowired
15     private IBookDao bookDao;
16 
17     @Override
18     public List<Book> listAll() {
19         return bookDao.listAll();
20     }
 1 package com.oukele.bookshop_ssm.web;
 2 
 3 import com.oukele.bookshop_ssm.entity.Book;
 4 import com.oukele.bookshop_ssm.service.BookServiceTemp;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.servlet.ModelAndView;
 9 
10 import java.util.List;
11 
12 @Controller
13 public class BookController {
14 
15     @Autowired
16     private BookServiceTemp bookServiceTemp;
17 
18 //    请求url
19     @RequestMapping(value = "/books")
20     public ModelAndView getList(){
21 //        页面的名字
22         ModelAndView mv = new ModelAndView("list");
23 //        获取数据
24         List<Book> list = bookServiceTemp.listAll();
25         mv.addObject("book",list);
26         return mv;
27     }
28 
29 }

 

(web)控制层

 1 package com.oukele.bookshop_ssm.web;
 2 
 3 import com.oukele.bookshop_ssm.entity.Book;
 4 import com.oukele.bookshop_ssm.service.BookService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.servlet.ModelAndView;
 9 
10 import java.util.List;
11 
12 @Controller
13 public class BookController {
14 
15     @Autowired
16     private BookService bookService;
17 
18 //    请求url
19     @RequestMapping(value = "/books")
20     public ModelAndView getList(){
21 //        页面的名字
22         ModelAndView mv = new ModelAndView("list");
23 //        获取数据
24         List<Book> list = bookService.listAll();
25         mv.addObject("book",list);
26         return mv;
27     }
28 
29 }

 

jsp页面

 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <%@ page contentType="text/html;charset=UTF-8" language="java"  isELIgnored="false" %>
 3 <html>
 4 <head>
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 书籍列表:
 9 <br>
10 
11 <table>
12     <tr>
13         <th>编号</th>
14         <th>书名</th>
15         <th>数量</th>
16     </tr>
17     <tbody>
18     <c:forEach items="${book}" var="books" >
19         <tr>
20             <td>${books.bookid}</td>
21             <td>${books.name}</td>
22             <td>${books.cnt}</td>
23         </tr>
24     </c:forEach>
25     </tbody>
26 </table>
27 
28 </body>
29 </html>

 

项目结构:

 

配置 web.xml文件

完整的web.xml文件信息

 1 <!DOCTYPE web-app PUBLIC
 2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4 
 5 <web-app>
 6   <display-name>Archetype Created Web Application</display-name>
 7   
 8   <!--配置 Spring 容器-->
 9   <context-param>
10     <param-name>contextConfigLocation</param-name>
11     <param-value>classpath:spring_root.xml</param-value>
12   </context-param>
13   <listener>
14     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
15   </listener>
16 
17   <!--配置 Spring mvc 容器-->
18   <!--将所有请求,都交给Spring MVC 处理-->
19   <servlet>
20     <servlet-name>webs</servlet-name>
21     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
22     <init-param>
23       <param-name>contextConfigLocation</param-name>
24       <param-value>classpath:spring_web.xml</param-value>
25     </init-param>
26   </servlet>
27   <!--将 请求 截取 转交给 webs -->
28   <servlet-mapping>
29     <servlet-name>webs</servlet-name>
30     <url-pattern>/</url-pattern>
31   </servlet-mapping>
32 
33 </web-app>

 配置tomcat服务器,并启动。

 

 

 

 

 

正文到此结束