SSM基本配置
先要导入基本的jar包,我这里用的是maven
<!-- 数据校验3030支持Hibernate-Validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<!-- 返回json字符串 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
<!-- 引入pageHelper分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
<!-- MBG -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- Spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- SpringMVC、Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- Spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- Spring面向切面 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- MyBatis整合Spring的适配包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
还有一些基本的插件
lombok可以快捷的生成get和set方法之类的
先配置web.xml中的一些基本
先配置spring的容器和springMVC的拦截器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcherServlet-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
这里还需要配置字符编码过滤器
不配置会乱码
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</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>
</web-app>
如果要使用Rest风格的url也需要配置
在web.xml中配置
<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>
<filter>
<filter-name>HttpPutFormContentFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HttpPutFormContentFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
现在配置applicationContext.xml,如下:
database.properties文件
jdbc.driver=com.mysql.jdbc.Driver
# 如果使用的是MySQL8.0+,增加一个时区的配置;&serverTimezone=Asia/shanghai
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=xiong
这个是applicationContext.xml文件
--先配置自动扫描--
<context:component-scan base-package="com.xiong">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
--导入上面**database.properties**外部资源--
<context:property-placeholder location="classpath:database.properties"/>
--这里用的是c3p0--
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="driverClass" value="${jdbc.driver}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
--配置MyBatis的整合--
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis全局配置文件的位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="pooledDataSource"></property>
<!-- 指定mybatis,mapper文件的位置 -->
<property name="mapperLocations" value="classpath:com.xiong.dao.*.xml"></property>
</bean>
<!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描所有dao接口的实现,加入到ioc容器中 -->
<property name="basePackage" value="com.xiong.dao"></property>
</bean>
--在配置事务控制器--
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 控制住数据源 -->
<property name="dataSource" ref="pooledDataSource"></property>
</bean>
--开启注解配置事务--
<aop:config>
<!-- 切入点表达式 -->
<aop:pointcut expression="execution(* com.xiong.service..*(..))" id="txPoint"/>
<!-- 配置事务增强 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!-- 配置事务增强,事务如何切入 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有方法都是事务方法 -->
<tx:method name="*"/>
<!-- 以get开始的所有方法 -->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
在配置mybatis-config.xml
--配置别名--
<typeAliases>
<package name="com.xiong.pojo"/>
</typeAliases>
--可以配置,也可以不配置--
引入这个pagehelper包就行,这个放在pom.xml中,使用maven创建的web项目
<--<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
这个删掉
-->
<!-- 引入分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 分页参数合理化 -->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
--可以配置,也可以不配置--
<!-- 开启驼峰命名法 -->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
在创建一个dispatcherServlet-servlet.xml文件
<!-- SpinrgMvc的配置文件,包含网站跳转逻辑的控制,配置 -->
<context:component-scan base-package="com.xiong" 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/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 俩个标准配置 -->
<!-- 将springmvc不能处理的请求交给tomcat -->
<mvc:default-servlet-handler/>
<!-- 能支持springMVC更高级的一些功能,JSP303校验,快捷的ajax... -->
<mvc:annotation-driven/>
我们写一个SpringMvc
SpringMVC相当于SSH中的student的
先创建一个Controller层,相当于Servlet
package com.xiong.controller;
import org.springframework.stereotype.Controller;
--这个注解必须要,要不然会报错--
@Controller
public class BookController {
//不要需要自己去创建set和get,但必须要注解
@Autowired // 这个是Service类
private StudentService service;
//这里面的getList是超链接访问的类
@RequestMapping("/getList")
public String getList() {
return "list";
// 这个是jsp页面list.jsp,是根据dispatcherServlet-servlet.xml中配置视图解析器中设置的
<!-- 配置视图解析器,方便页面返回
这个是dispatcherServlet-servlet.xml里面的
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
这个要删掉
-->
}
}
这个是service层
package dispatcherServlet-servlet.xml
@Service
public class StudentServiceImpl(){
// 这个是dao层,这个注解必须要,要不然扫描不到
@Autowired
private StudentMapper mapper;
内容
..............
}
dao层,使用MyBatis框架
package dispatcherServlet-servlet.xml
public interface StudentMapper(){
/**
* 模糊查询或者查询
* @param student
* @return
*/
List<Student> selectStudent(@Param("student")Student student);
/**
* 添加
* @param student
* @return
*/
Integer insertStudent(Student student);
/**
* 修改
* @param student
* @return
*/
Integer updateStudent(Student student);
/**
* 刪除
* @param ids
* @return
*/
Integer deleteStudent(Integer[] ids);
}
这个是dao层中StudentMapper.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.xiong.dao.StudentMapper">
<sql id="sql_name">
id,name,sex,age
</sql>
<!-- 查询或者模糊查询 -->
<select id="selectStudent" resultType="student" resultSets="java.util.List">
select <include refid="sql_name"></include> from student
<where>
<if test="student !=null and student.name !=null and student.name !='' ">
name like concat("%",#{student.name},"%")
</if>
<if test="student !=null and student.sex !=null">
and age = #{student.age}
</if>
<if test="student !=null and student.sex !=null and student.sex !='' ">
and sex = #{student.sex}
</if>
<if test="student !=null and student.id !=null">
and id = #{student.id}
</if>
</where>
</select>
<!-- 添加 -->
<insert id="insertStudent" useGeneratedKeys="true" keyProperty="id">
insert into student(name,sex,age) values (#{name},#{sex},#{age})
</insert>
<!-- 修改 -->
<update id="updateStudent">
update student
<set>
<if test="name !=null and name !='' ">
name = #{name},
</if>
<if test="sex !=null and sex !='' ">
sex = #{sex},
</if>
<if test="age !=null">
age = #{age}
</if>
where id = #{id}
</set>
</update>
<!-- 刪除或者多个数据删除 -->
<delete id="deleteStudent">
delete from student
where id in
<foreach collection="array" item="id" open="(" separator="," close=")">#{id}</foreach>
</delete>
</mapper>
这是我第一次写的博客,写的应给还不错!!!
给个赞