SSM整合
1.导入需要的jar包
导入全局属性
<properties>
<spring_data_version>4.3.8.RELEASE</spring_data_version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
在pom.xml中,配置如下jar包
<dependencies>
<!--这个是自带的测试包,版本改为4.12 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ognl/ognl -->
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.1.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4
j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist
-->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.22.0-CR2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12
-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-j
ava -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spr
ing-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring_data_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring_data_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring_data_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring_data_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring_data_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring_data_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring_data_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring_data_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring_data_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring_data_version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring_data_version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.3.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
<!--springMVC需要的jar包:spring-core,spring-webmvc,jstl-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- SpringMVC需要的jar包导入完毕 -->
<!-- 使用requestBody和responseBody需要依赖的jar包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.6</version>
</dependency>
为了能在mybatis的配置文件中,注册指定包下的配置文件,需要在标签中添加如下
<resources>
<!--添加也打包java下的-->
<resource>
<directory>src/main/java</directory>
<includes>
<!--要加入的文件的后缀名-->
<!--*.properties的*:任意文件-->
<!--**/:任意目录下-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<!-- 是否替换资源中的属性-->
<filtering>false</filtering>
</resource>
<!--默认只打包resources的-->
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
配置Spring的配置文件
创建spring-config.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" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- mvc的注解 -->
<mvc:annotation-driven/>
<!--
将静态资源,交由web处理
SpringMVC不管
-->
<mvc:default-servlet-handler/>
<!-- SpringMVC:处理请求转发 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 自动扫描 -->
<context:component-scan base-package="com.pgg"/>
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.driver}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="user" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="initialPoolSize" value="${db.initialPoolSize}"/>
<!--最大空闲时间,池中连接对象,长时间不使用则被丢弃-->
<!--默认:0,不丢弃-->
<property name="maxIdleTime" value="${db.maxIdleTime}"/>
<property name="maxPoolSize" value="${db.maxPoolSize}"/>
<property name="minPoolSize" value="${db.minPoolSize}"/>
<!--若池中无空闲连接,则一次性增加多少连接对象-->
<!--默认:3-->
<property name="acquireIncrement" value="${db.acquireIncrement}"/>
<!--两次获取连接的间隔时间-->
<!--默认:1000毫秒-->
<property name="acquireRetryDelay" value="${db.acquireRetryDelay}"/>
<!--若获取连接失败,尝试多少次-->
<!--默认:30次-->
<property name="acquireRetryAttempts" value="${db.acquireRetryAttempts}"/>
<!--获取连接失败,则会引起所有正在等待连接池给予连接的线程,抛出异常-->
<!--但是数据源扔有效保留,并在下次调用getConnection时,继续尝试获取连接-->
<!--如果设置为true,那么在尝试获取连接失败后,会将数据源设置为已断开状态并永久关闭-->
<!--默认:false-->
<property name="breakAfterAcquireFailure" value="${db.breakAfterAcquireFailure}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--mybatis的配置文件路径-->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
<!--在spring的配置文件中,为mybatis指定数据源-->
<!--就是我们上面定义的c3p0的dataSource-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--这里和ssh时一样,配置事务管理对象-->
<!--切面-->
<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="add*"/>
<tx:method name="delete*"/>
<tx:method name="update*"/>
<tx:method name="find*"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pointCut" expression="execution(* com.pgg.*.dao.*.*(..))"/>
<!--
配置通知
就是在切入点前后要做的事情(前置通知后置通知什么的
-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>
<!--由spring管理原始dao-->
<!--方式一-->
<!--<bean id="userDao" class="com.pgg">-->
<!--<property name="sqSessionFactory" ref="sqlSessionFactory"/>-->
<!--</bean>-->
<!--方式二-->
<!--通过mapper代理开发模式,批量进行mapper配置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定批量mapper配置的包名(就是dao层)-->
<property name="basePackage" value="com.pgg.*.dao"/>
<!--指定这些dao都使用哪个SqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
配置mybatis的配置文件
创建一个名称为SqlMapConfig.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>
<!--设置全局参数-->
<settings>
<!-- lazyLoadingEnabled:延迟加载的开关,默认为false -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- aggressiveLazyLoading默认为true,一旦为true,则上面的懒加载开关将失效 -->
<setting name="aggressiveLazyLoading" value="false"/>
<!-- cacheEnabled:二级缓存的总开关 默认为false -->
<setting name="cacheEnabled" value="true"/>
</settings>
<!-- 配置别名 -->
<typeAliases>
<package name="com.pgg.user.domain"/>
</typeAliases>
<mappers>
<!--注册指定包下的所有配置文件-->
<package name="com.pgg.*.dao"/>
</mappers>
</configuration>
创建dbcp连接池的配置文件db.properties
# 配置四大参数
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/mybatis
db.username=root
db.password=123
# 池配置
db.initialPoolSize=20
db.maxIdleTime=60
db.maxPoolSize=200
db.minPoolSize=50
db.acquireIncrement=3
db.acquireRetryDelay=1000
db.acquireRetryAttempts=30
db.breakAfterAcquireFailure=false
在java文件夹下,创建com.pgg.user包
测试文件如下
package com.pgg.user.controller;
import com.pgg.user.dao.UserMapper;
import com.pgg.user.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
@Controller
@RequestMapping("/user") // 相当于命名空间
public class UserController {
@Resource
private UserMapper userMapper;
@RequestMapping("/tor")
public String toRegister(){
return "register";
}
@RequestMapping("/register")
public String register(User user){
userMapper.insertUser(user);
return "redirect:/user/login";
}
@RequestMapping("/login")
public String login(){
return "login";
}
}
UserMapper
package com.pgg.user.dao;
import com.pgg.user.domain.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
User findUserById(int id);
void insertUser(User user);
}
UserMapper.xml
<?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.pgg.user.dao.UserMapper">
<select id="findUserById" parameterType="int" resultType="user">
SELECT * FROM USER WHERE id = #{id}
</select>
<insert id="insertUser" parameterType="user" keyColumn="id" useGeneratedKeys="true">
INSERT INTO USER (id,username) VALUES (#{id},#{username})
</insert>
</mapper>
User
package com.pgg.user.domain;
public class User {
private int id;
private String username;
public User() {
}
public User(int id, String username) {
this.id = id;
this.username = username;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
'}';
}
}