SSM整合
1.导包:
<!--支持junit测试坐标-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--支持SpringIOC的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--支持Spring中AOP的坐标-->
<dependency>
<groupId>org.sonatype.sisu.inject</groupId>
<artifactId>cglib</artifactId>
<version>2.2.1-v20090111</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!--Spring的jdbc核心-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<!--Spring的测试核心-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<!--支持SpringMVC的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<!--支持jsp的坐标-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!--支持文件上传与下载的坐标-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!--支持json的坐标-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.3</version>
</dependency>
<!--数据校验的坐标-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.4.1.Final</version>
</dependency>
<!--支持JSTL的坐标-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--支持验证码的坐标-->
<!--支持mybatis与spring的整合坐标-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--支持mybatis的坐标-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--支持数据库驱动的坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!--支持数据源的坐标-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--支持ehcache的坐标-->
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<!--支持分页所需要的坐标-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<!--支持分页的sql解析工具坐标-->
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>1.0</version>
</dependency>
2.写配置:
- 配置web.xml文件
<!--配置Spring容器启动-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--指定Spring配置文件的位置-->
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置SpringMVC的前端控制器-->
<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:spring/applicationContext-mvc.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>
<!--配置字符编码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--处理post请求的编码问题-->
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<!--处理响应的编码问题-->
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--处理Rest风格的过滤器-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 编写SpringMVC的配置文件:applicationContext-mvc.xml:
<!--开启组件扫描-->
<context:component-scan base-package="com" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--开启静态资源访问权限-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--开启动态资源访问权限-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置文件上传下载-->
<!--id一定要指定为:multipartResolver-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSizePerFile" value="#{1024*1024*20}"></property>
</bean>
2.编写spring的核心配置文件:applicationContext.xml
<!--开启组件扫描-->
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--0.导入外部文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--1.配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
</bean>
<!--3.配置使用mybatis操作数据库-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--指定配置文件的位置,需要加classpath:代表类路径下的位置-->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
<!--指定xml映射文件的位置,需要加classpath:代表类路径下的位置-->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
</bean>
<!--我们要把每一个dao接口的实现加入到ioc容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定dao接口所在的包-->
<property name="basePackage" value="com.dao"></property>
</bean>
<!--4.配置事务控制,配置事务管理器,让他控制住数据源里面的连接的关闭和提交-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--5.基于xml配置,配置事务,哪些方法切入事务还要写切点表达式-->
<aop:config>
<aop:pointcut id="txPoint" expression="execution(* com.service.*.*(..))"/>
<aop:advisor advice-ref="myTx" pointcut-ref="txPoint"></aop:advisor>
</aop:config>
<!--6.配置事务增强、事务属性、事务建议-->
<tx:advice id="myTx" transaction-manager="transactionManager">
<tx:attributes>
<!--指定所有方法都切入事务,出现异常就回滚-->
<!-- <tx:method name="*" rollback-for="java.lang.Exception"/>-->
<tx:method name="get*" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
3.编写mybatis的核心配置文件:mybatis-config.xml
<configuration>
<properties resource="jdbc.properties"></properties>
<settings>
<!--开启全局缓存开关-->
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
4.编写sql映射文件:TeacherDao.xml
<mapper namespace="com.dao.TeacherDao">
<resultMap id="myTeacher" type="com.pojo.Teacher">
<id property="id" column="id"></id>
<result property="name" column="teacherName"></result>
<result property="course" column="class_name"></result>
<result property="address" column="address"></result>
<result property="date" column="birth_date"></result>
</resultMap>
<!--public Teacher getTeacherById(Integer id);-->
<select id="getTeacherById" resultMap="myTeacher" >
select * from t_teacher where id = #{id}
</select>
<update id="updateTeacher" flushCache="true">
update t_teacher
<set>
<if test="name!=null and !name.equals('')">
teacherName=#{name},
</if>
<if test="course!=null and !course.equals('')">
class_name=#{course}
</if>
</set>
<where>
id=#{id}
</where>
</update>
<!--public void insertTeachers(List<Teacher> teacherList);-->
<insert id="insertTeachers">
INSERT INTO t_teacher (teacherName,class_name)
VALUES
<foreach collection="list" item="teacher" separator=",">
(#{teacher.name},#{teacher.course})
</foreach>
</insert>
<!--public List<Teacher> getTeachers();-->
<select id="getTeachers" resultMap="myTeacher">
select * from t_teacher
</select>
</mapper>
5.编写controller类:
@Controller
public class TeacherController {
@Autowired
private TeacherService teacherService;
@RequestMapping("/haha")
public String handle01(@RequestParam(value = "id") Integer id, Model model){
Teacher teacher = teacherService.getTeacherById(id);
model.addAttribute("teacher",teacher);
return "success";
}
}
6.编写service类:
@Service
public class TeacherService {
@Autowired
private TeacherDao teacherDao;
public Teacher getTeacherById(Integer id){
return teacherDao.getTeacherById(id);
}
}
7.编写dao接口:
public interface TeacherDao {
public Teacher getTeacherById(Integer id);
}
3.测试:
3.1 通过index.jsp页面来点击超链接来进行跳转:
<%@page contentType="text/html;charset=utf-8" language="java" %>
<html>
<body>
<a href="haha?id=1">查询老师!!!</a><br/>
</body>
</html>
3.2 成功之后跳转到success.jsp页面,展示出查询出来的数据