本筆記是在學(xué)習(xí)狂神說的SpringMVC時(shí)創(chuàng)建椎椰。
個(gè)人博客 : Dexter
SSM整合步驟
1.環(huán)境
IDEA
JDK 1.8
Tomcat 9
Maven3.6
Mysql 5.7
數(shù)據(jù)庫環(huán)境
創(chuàng)建圖書表
CREATE DATABASE `ssmbuild` ;
CREATE TABLE `books` (
`bookID` int(10) NOT NULL AUTO_INCREMENT COMMENT '書id',
`bookName` varchar(100) NOT NULL COMMENT '書名',
`bookCounts` int(11) NOT NULL COMMENT '數(shù)量',
`detail` varchar(200) NOT NULL COMMENT '描述',
KEY `bookID` (`bookID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
導(dǎo)入數(shù)據(jù)略......
2.整合開始
1.打開IDEA創(chuàng)建普通Maven項(xiàng)目并添加wed支持
ssm01.png
2.添加相關(guān)依賴
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--數(shù)據(jù)庫驅(qū)動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 數(shù)據(jù)庫連接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
注意:靜態(tài)資源過濾問題
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
3.創(chuàng)建基本結(jié)構(gòu)
ssm02.png
ssm03.png
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>
</configuration>
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">
</beans>
4. Mybatis層
(1)創(chuàng)建實(shí)體類
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
public Books()
{
}
}
(2)創(chuàng)建BookMapper接口
package com.chao.mapper;
import com.chao.pojo.Books;
import java.util.List;
public interface BookMapper {
//增加一本書
int addBook(Books book);
//刪除一本書
int deleteBook(int id);
//修改一本書
int updateBook(Books book);
//查詢一本書
Books queryBookById(int id);
//查詢所有的書
List<Books> queryAllBook();
}
(2)創(chuàng)建BookMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.chao.mapper.BookMapper">
<!--增加一本書 -->
<insert id="addBook" parameterType="Books">
insert into ssmbuild.books(bookName, bookCounts, detail)
values(#{bookName},#{bookName},#{detail});
</insert>
<!--刪除一本書 -->
<delete id="deleteBook" parameterType="int">
delete from ssmbuild.books
where bookID=#{id};
</delete>
<!--修改一本書 -->
<update id="updateBook" parameterType="Books">
update ssmbuild.books set bookName=#{bookName},bookCounts=#{bookCounts}
,detail=#{detail} where bookID=#{bookID} ;
</update>
<!--查詢一本書根據(jù)ID-->
<select id="queryBookById" parameterType="int" resultType="Books">
select * from ssmbuild.books where bookID=#{bookId};
</select>
<!--查詢所有的書 -->
<select id="queryAllBook" resultType="Books">
select * from ssmbuild.books;
</select>
</mapper>
(3)數(shù)據(jù)庫外部文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
(4)Mybatis核心文件
<?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.chao.pojo"/>
</typeAliases>
<mappers>
<mapper resource="com/chao/dao/BookMapper.xml"/>
</mappers>
</configuration>
5.編寫業(yè)務(wù)層
(1)創(chuàng)建Service接口
package com.chao.Service;
import com.chao.pojo.Books;
import java.util.List;
public interface BookService {
//增加一本書
int addBook(Books book);
//刪除一本書
int deleteBook(int id);
//修改一本書
int updateBook(Books book);
//查詢一本書
Books queryBookById(int id);
//查詢所有的書
List<Books> queryAllBook();
}
(2)編寫實(shí)現(xiàn)類
package com.chao.Service;
import com.chao.mapper.BookMapper;
import com.chao.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public int addBook(Books book) {
return bookMapper.addBook(book);
}
@Override
public int deleteBook(int id) {
return bookMapper.deleteBook(id);
}
@Override
public int updateBook(Books book) {
return bookMapper.updateBook(book);
}
@Override
public Books queryBookById(int id) {
return bookMapper.queryBookById(id);
}
@Override
public List<Books> queryAllBook() {
return bookMapper.queryAllBook();
}
}
6.Spring整合
(1)Spring整合Dao層入录,創(chuàng)建Spring-Dao.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合mybatis -->
<!-- 1.關(guān)聯(lián)數(shù)據(jù)庫文件 -->
<context:property-placeholder location="database.properties"></context:property-placeholder>
<!-- 2.數(shù)據(jù)庫連接池 -->
<!--數(shù)據(jù)庫連接池
dbcp 半自動化操作 不能自動連接
c3p0 自動化操作(自動的加載配置文件 并且設(shè)置到對象里面)
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置連接池屬性 -->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- c3p0連接池的私有屬性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 關(guān)閉連接后不自動commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 獲取連接超時(shí)時(shí)間 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 當(dāng)獲取連接失敗重試次數(shù) -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory對象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入數(shù)據(jù)庫連接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Mybatis全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 4.配置掃描Dao接口包,動態(tài)實(shí)現(xiàn)Dao接口注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 給出需要掃描Dao接口包 -->
<property name="basePackage" value="com.chao.mapper"/>
</bean>
</beans>
(2)Spring整合Service層
<?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"
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">
<!-- 掃描service相關(guān)的bean -->
<context:component-scan base-package="com.chao.Service" />
<!-- 配置事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入數(shù)據(jù)庫連接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
(3)Spring整合MVC層
<?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">
<!-- 配置SpringMVC -->
<!-- 1.開啟SpringMVC注解驅(qū)動 -->
<mvc:annotation-driven/>
<!-- 2.靜態(tài)資源默認(rèn)servlet配置-->
<mvc:default-servlet-handler/>
<!-- 3.配置jsp 顯示ViewResolver視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 4.掃描web相關(guān)的bean -->
<context:component-scan base-package="com.chao.controller"/>
</beans>
(4)整合controller層
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--DispatcherServlet-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--一定要注意:我們這里加載的是總的配置文件精置,之前被這里坑了!-->
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--encodingFilter-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Session過期時(shí)間-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
5)Spring配置整合文件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="Spring-Dao.xml"/>
<import resource="Spring-service.xml"/>
<import resource="spring-mvc.xml"/>
</beans>
3.整合結(jié)束
下面可以進(jìn)行視圖層的編寫!<坡丁著榴!