基本环境搭建
1、导入SSM框架以及所必需的依赖
<!--依赖:junit,数据库驱动,连接池,servlet,jsp,mybatis,mybatis-spring,spring-->
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</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.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
<!--json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
</dependencies>
<!--Maven资源过滤设置-->
<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>
2、建立基本结构和配置框架!
-com.bjpowernode.pojo
-com.bjpowernode.dao
-com.bjpowernode.service
-com.bjpowernode.controller
-com.bjpowernode.listener
-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>
Mybatis层编写
1、数据库配置文件
jdbc.driver=com.mysql.cj.jdbc.Driver
#mysql8.0以上需要加一个时区的配置:&serverTimezone=Aisa/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=130848
2、IDEA关联数据库
3、编写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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.bjpowernode.pojo"/>
</typeAliases>
<mappers>
<mapper resource="com/bjpowernode/dao/BooksMapper.xml"/>
</mappers>
</configuration>
4、编写实体类
@Data//这里使用了lombok
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
5、编写Dao层的Mapper接口
public interface BooksMapper {
//增
int addBooks(Books books);
//删
int deleteBooks(@Param("bookId") int bookId);
//改
int updateBooks(Books books);
//查
Books queryBooksById(@Param("bookId") int bookId);
List<Books> queryBooks();
//通过name查找书籍
Books queryBooksByName(@Param("bookName") String bookName);
}
6、编写mapper接口所对应的Mapper.xml文件(需要导入MyBatis的包)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjpowernode.dao.BooksMapper">
<insert id="addBooks" parameterType="Books">
insert into ssmbuild.books (bookName, bookCounts, detail) values
(#{bookName},#{bookCounts},#{detail})
</insert>
<delete id="deleteBooks" parameterType="_int">
delete from ssmbuild.books where bookID = #{bookId}
</delete>
<update id="updateBooks" parameterType="Books">
update ssmbuild.books
<set>
<if test="bookName != null">
bookName = #{bookName},
</if>
<if test="bookCounts != null">
bookCounts = #{bookCounts},
</if>
<if test="detail != null">
detail = #{detail}
</if>
</set>
where bookID = #{bookID}
</update>
<select id="queryBooksById" parameterType="_int" resultType="Books">
select * from ssmbuild.books where bookID = #{bookId}
</select>
<select id="queryBooks" parameterType="_int" resultType="Books">
select * from ssmbuild.books
</select>
<select id="queryBooksByName" resultType="Books">
select * from ssmbuild.books where bookName = #{bookName}
</select>
</mapper>
7、编写Service层的接口和实现类
接口:
public interface BooksService {
//增
int addBooks(Books books);
//删
int deleteBooks(@Param("bookId") int bookId);
//改
int updateBooks(Books books);
//查
Books queryBooksById(@Param("bookId") int bookId);
List<Books> queryBooks();
//通过name查找书籍
Books queryBooksByName(@Param("bookName") String bookName);
}
实现类:
public class BooksServiceImpl implements BooksService{
private BooksMapper mapper;
public void setMapper(BooksMapper mapper) {
this.mapper = mapper;
}
@Override
public int addBooks(Books books) {
return mapper.addBooks(books);
}
@Override
public int deleteBooks(int bookId) {
return mapper.deleteBooks(bookId);
}
@Override
public int updateBooks(Books books) {
return mapper.updateBooks(books);
}
@Override
public Books queryBooksById(int bookId) {
return mapper.queryBooksById(bookId);
}
@Override
public List<Books> queryBooks() {
return mapper.queryBooks();
}
@Override
public Books queryBooksByName(String bookName) {
return mapper.queryBooksByName(bookName);
}
}
spring层:
1、配置spring整合mybatis,这里数据源使用c3p0连接池;
2、编写Spring整合Mybatis的相关配置文件:spring-dao.xml
在spring-dao.xml中需要配置:
1.需要导入数据库配置文件:database.properties
2.配置连接池
3.配置sqlSessionFactory,并连接到mybatis配置文件
4.配置扫描dao层的接口,动态实现dao接口注入到spring容器中
<?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">
<!--导入数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!--连接池
dbcp:半自动化操作,不能自动连接
c3p0:自动化操作(自动加载配置文件,并且可以自动设置到对象中)
durid: hikari:
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<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"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--配置dao接口扫描包,动态的实现了Dao接口可以注入到spring容器中!-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--要扫描的包-->
<property name="basePackage" value="com.bjpowernode.dao"/>
</bean>
</beans>
3、spring整合service层
在spring-service中需要配置:
1.扫描service层的包
2.将dao层的mapper注入到service(也可通过注解实现)
3.声明式事务
4.配置aop事务切面(可选)
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--1.扫描service下的包-->
<context:component-scan base-package="com.bjpowernode.service"/>
<!--2.将我们的所有业务类注入到Spring,可以通过配置或者注解实现-->
<bean id="BooksServiceImpl" class="com.bjpowernode.service.BooksServiceImpl">
<property name="mapper" ref="booksMapper"/>
</bean>
<!--声明式事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--aop事务支持-->
<aop:config>
<aop:pointcut id="txPoincut" expression="execution(* com.bjpowernode.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoincut"/>
</aop:config>
</beans>
springMVC层
1、web.xml
<?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>springmvc</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>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--乱码过滤-->
<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过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
<listener>
<listener-class>com.bjpowernode.listener.MyListener</listener-class>
</listener>
</web-app>
2、spring-mvc.xml
在spring-mvc中需要配置:
1.开启注解驱动
2.静态资源过滤
3.配置视图解析器
4.扫描controller包
<?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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--注解驱动-->
<mvc:annotation-driven/>
<!--静态资源过滤-->
<mvc:default-servlet-handler/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--扫描包-->
<context:component-scan base-package="com.bjpowernode.controller"/>
</beans>
3、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" xmlns:mvc="http://www.springframework.org/schema/context"
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">
<import resource="spring-mvc.xml"/>
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
</beans>
4、编辑监听器,以防止tomcat关闭时的JDBC连接关闭失败
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("Web start");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("Web stop");
try {
while (DriverManager.getDrivers().hasMoreElements()) {
DriverManager.deregisterDriver(DriverManager.getDrivers().nextElement());
}
System.out.println("JDBC Driver Close");
AbandonedConnectionCleanupThread.checkedShutdown();
System.out.println("clean thread success");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
至此为止,SSM框架就搭好了,剩下的就是编写业务的实现以及前端界面。