SSM框架整合

(数据库)

一.导入包 pom.xml里面的配置

 1    <dependencies>
 2         <!--Junit-->
 3         <dependency>
 4             <groupId>junit</groupId>
 5             <artifactId>junit</artifactId>
 6             <version>4.12</version>
 7         </dependency>
 8         <!--数据库驱动-->
 9         <dependency>
10             <groupId>mysql</groupId>
11             <artifactId>mysql-connector-java</artifactId>
12             <version>8.0.18</version>
13         </dependency>
14         <!-- 数据库连接池 -->
15         <dependency>
16             <groupId>com.mchange</groupId>
17             <artifactId>c3p0</artifactId>
18             <version>0.9.5.2</version>
19         </dependency>
20 
21         <!--Servlet - JSP -->
22         <dependency>
23             <groupId>javax.servlet</groupId>
24             <artifactId>servlet-api</artifactId>
25             <version>2.5</version>
26         </dependency>
27         <dependency>
28             <groupId>javax.servlet.jsp</groupId>
29             <artifactId>jsp-api</artifactId>
30             <version>2.2</version>
31         </dependency>
32         <dependency>
33             <groupId>javax.servlet</groupId>
34             <artifactId>jstl</artifactId>
35             <version>1.2</version>
36         </dependency>
37 
38         <!--Mybatis-->
39         <dependency>
40             <groupId>org.mybatis</groupId>
41             <artifactId>mybatis</artifactId>
42             <version>3.5.2</version>
43         </dependency>
44         <dependency>
45             <groupId>org.mybatis</groupId>
46             <artifactId>mybatis-spring</artifactId>
47             <version>2.0.2</version>
48         </dependency>
49 
50         <!--Spring-->
51         <dependency>
52             <groupId>org.springframework</groupId>
53             <artifactId>spring-webmvc</artifactId>
54             <version>5.1.9.RELEASE</version>
55         </dependency>
56         <dependency>
57             <groupId>org.springframework</groupId>
58             <artifactId>spring-jdbc</artifactId>
59             <version>5.1.9.RELEASE</version>
60         </dependency>
61     </dependencies>
62 
63     <build>
64         <resources>
65             <resource>
66                 <directory>src/main/java</directory>
67                 <includes>
68                     <include>**/*.properties</include>
69                     <include>**/*.xml</include>
70                 </includes>
71                 <filtering>false</filtering>
72             </resource>
73             <resource>
74                 <directory>src/main/resources</directory>
75                 <includes>
76                     <include>**/*.properties</include>
77                     <include>**/*.xml</include>
78                 </includes>
79                 <filtering>false</filtering>
80             </resource>
81         </resources>
82     </build>
83 
84     <properties>
85         <maven.compiler.source>1.8</maven.compiler.source>
86         <maven.compiler.target>1.8</maven.compiler.target>
87     </properties>

 

二.创建包

  

  1.controller          springmvc的

          2.dao                  mybatis的

      

      

 1 package com.bjpowernode.dao;
 2 
 3 import com.bjpowernode.pojo.Books;
 4 import org.apache.ibatis.annotations.Param;
 5 
 6 import java.util.List;
 7 
 8 public interface BookMapper {
 9     //增加一本书
10     int addBook(Books books);
11     //删除一本书
12     int deleteBookById(@Param("bookId") int id);
13     //更新一本书
14     int updateBook(Books books);
15     //查询一本书
16     Books queryBookById(@Param("bookId") int id);
17     //查询全部的书
18     List<Books> queryAllBook();
19 }

         

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.bjpowernode.dao.BookMapper">
 6     <insert id="addBook">
 7         insert into books(bookName,bookCounts,detail)values(#{bookName},#{bookCounts},#{detail})
 8     </insert>
 9     <delete id="deleteBookById">
10         delete from books where bookId=#{bookId}
11     </delete>
12     <update id="updateBook">
13         update books set #{bookName},#{bookCounts},#{detail} where bookId = #{bookId}
14     </update>
15     <select id="queryBookById" resultType="Books">
16         select * from books where bookId=#{bookId}
17     </select>
18     <select id="queryAllBook" resultType="Books">
19         select * from books
20     </select>
21 </mapper>

   3.pojo         实体类

    类名就是表名

  

 1 package com.bjpowernode.pojo;
 2 
 3 //数据库实体类
 4 public class Books {
 5     private int bookID;
 6     private String bookName;
 7     private int bookCounts;
 8     private String detail;
 9 
10     public Books() {
11     }
12 
13     public Books(int bookID, String bookName, int bookCounts, String detail) {
14         this.bookID = bookID;
15         this.bookName = bookName;
16         this.bookCounts = bookCounts;
17         this.detail = detail;
18     }
19 
20     public int getBookID() {
21         return bookID;
22     }
23 
24     public void setBookID(int bookID) {
25         this.bookID = bookID;
26     }
27 
28     public String getBookName() {
29         return bookName;
30     }
31 
32     public void setBookName(String bookName) {
33         this.bookName = bookName;
34     }
35 
36     public int getBookCounts() {
37         return bookCounts;
38     }
39 
40     public void setBookCounts(int bookCounts) {
41         this.bookCounts = bookCounts;
42     }
43 
44     public String getDetail() {
45         return detail;
46     }
47 
48     public void setDetail(String detail) {
49         this.detail = detail;
50     }
51 
52     @Override
53     public String toString() {
54         return "Books{" +
55                 "bookID=" + bookID +
56                 ", bookName='" + bookName + '\'' +
57                 ", bookCounts=" + bookCounts +
58                 ", detail='" + detail + '\'' +
59                 '}';
60     }
61 }

    

   4.service         spring的

  

 1 package com.bjpowernode.service;
 2 
 3 import com.bjpowernode.pojo.Books;
 4 import org.apache.ibatis.annotations.Param;
 5 
 6 import java.util.List;
 7 
 8 public interface BookService {
 9     //增加一本书
10     int addBook(Books books);
11     //删除一本书
12     int deleteBookById(int id);
13     //更新一本书
14     int updateBook(Books books);
15     //查询一本书
16     Books queryBookById(int id);
17     //查询全部的书
18     List<Books> queryAllBook();
19 }

     

 1 package com.bjpowernode.service;
 2 
 3 import com.bjpowernode.dao.BookMapper;
 4 import com.bjpowernode.pojo.Books;
 5 
 6 import java.util.List;
 7 
 8 public class BookServiceImpl implements BookService {
 9     //业务层(Service)调用dao层
10     private BookMapper bookMapper;
11 
12     public void setBookMapper(BookMapper bookMapper) {
13         this.bookMapper = bookMapper;
14     }
15 
16     @Override
17     public int addBook(Books books) {
18         return bookMapper.addBook(books);
19     }
20 
21     @Override
22     public int deleteBookById(int id) {
23         return bookMapper.deleteBookById(id);
24     }
25 
26     @Override
27     public int updateBook(Books books) {
28         return bookMapper.updateBook(books);
29     }
30 
31     @Override
32     public Books queryBookById(int id) {
33         return bookMapper.queryBookById(id);
34     }
35 
36     @Override
37     public List<Books> queryAllBook() {
38         return bookMapper.queryAllBook();
39     }
40 }

 

三.配置

    

 

  1.applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--把配置放到一起统一管理-->
    <import resource="classpath:spring-dao.xml"></import>
    <import resource="classpath:spring-serivce.xml"></import>
    <import resource="classpath:spring-mvc.xml"></import>
</beans>

       2.db.properties 数据库连接

       3.mybatis-config.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>
   <typeAliases>
       <package name="com.bjpowernode.pojo"/>
   </typeAliases>
    <mappers>
        <mapper class="com.bjpowernode.mapper.BookMapper"></mapper>
    </mappers>
</configuration>

       4.spring-dao.xml      相当于mybatis配置

  

 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:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context
 8         https://www.springframework.org/schema/context/spring-context.xsd">
 9     <!--spring层  整合Mybaits层-->
10     <!--关联数据库配置文件-->
11     <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
12 
13     <!--连接池
14         c3p0:自动化操作(自动化的加载配置文件,并且可以自动设置到对象当中)
15     -->
16     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
17         <property name="driverClass" value="${driver}"></property>
18         <property name="jdbcUrl" value="${url"></property>
19         <property name="user" value="${username}"></property>
20         <property name="password" value="${password}"></property>
21 
22         <!--c3p0连接池的私有属性-->
23         <property name="maxPoolSize" value="30"></property>
24         <property name="minPoolSize" value="10"></property>
25         <!--关闭连接后不自动commit(提交)-->
26         <property name="autoCommitOnClose" value="false"></property>
27         <!--获取连接超时时间-->
28         <property name="checkoutTimeout" value="10000"></property>
29         <!--当获取连接失败重试次数-->
30         <property name="acquireRetryAttempts" value="2"></property>
31     </bean>
32 
33     <!--sqlSessionFactory-->
34     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
35         <property name="dataSource" ref="dataSource"></property>
36         <!--绑定Mybatis的配置文件-->
37         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
38     </bean>
39 
40     <!--配置dao接口扫描包,动态的实现了Dao接口可以注入到Spring容器中-->
41     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
42         <!--注入 sqlSessionFactory-->
43         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
44         <!--要扫描的dao包-->
45         <property name="basePackage" value="com.bjpowernode.dao"></property>
46     </bean>
47 
48 
49 </beans>

          5.spring-service   Spring配置

  

 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:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6     http://www.springframework.org/schema/beans/spring-beans.xsd
 7     http://www.springframework.org/schema/context
 8     http://www.springframework.org/schema/context/spring-context.xsd">
 9     <!--spring的业务层service-->
10 
11     <!--扫描service下的包-->
12     <context:component-scan base-package="com.bjpowernode.service"></context:component-scan>
13 
14     <!--将我们的所有业务类,注入到Spring 可以通过配置,或者注解实现-->
15     <bean id="BookServiceImpl" class="com.bjpowernode.service.BookServiceImpl">
16         <property name="bookMapper" ref="bookMapper"></property>
17     </bean>
18 
19     <!--声明试事务配置-->
20     <!--导入这个已经有事务功能了,但是没有横切(具体)到哪个方法当中-->
21     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
22         <!--注入数据源-->
23         <property name="dataSource" ref="dataSource"></property>
24     </bean>
25 </beans>

   6.spring-mvc        SpringMvc配置

   

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--扫描包-->
    <context:component-scan base-package="com.bjpowernode.controller"></context:component-scan>
    <!--静态资源过滤-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    <!--注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

        7.web.xml配置

  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5          version="4.0">
 6     <!--DispatcherServlet-->
 7     <servlet>
 8         <servlet-name>springmvc</servlet-name>
 9         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
10         <init-param>
11             <param-name>contextConfigLocation</param-name>
12             <param-value>classpath:applicationContext.xml</param-value>
13         </init-param>
14         <load-on-startup>1</load-on-startup>
15     </servlet>
16     <servlet-mapping>
17         <servlet-name>springmvc</servlet-name>
18         <url-pattern>/</url-pattern>
19     </servlet-mapping>
20 
21     <!--乱码过滤-->
22     <filter>
23         <filter-name>encodingFilter</filter-name>
24         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
25         <init-param>
26             <param-name>encoding</param-name>
27             <param-value>utf-8</param-value>
28         </init-param>
29     </filter>
30     <filter-mapping>
31         <filter-name>encodingFilter</filter-name>
32         <url-pattern>/*</url-pattern>
33     </filter-mapping>
34 </web-app>

      8.创建一个jsp

  

 

posted @ 2020-03-27 18:51  春天春天  阅读(120)  评论(0编辑  收藏  举报
Document