SSM整合开发
1、搭建SSM开发环境
(1)在pom.xml中加入依赖和插件
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2.1-b03version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>5.2.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.3.RELEASEversion>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.1version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.48version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.1version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
<plugins>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
(2)配置web.xml文件
A、注册ContextLoaderListener监听器
- 注册 ServletContext 监听器的实现类 ContextLoaderListener,用于创建 Spring 容器及将创建好的 Spring 容器对象放入到 ServletContext 的作用域中。
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:conf/applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
B、注册CharacterEncodingFilter字符集过滤器
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
<init-param>
<param-name>forceRequestEncodingparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>forceResponseEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
C、配置DispatcherServlet中央调度器
<servlet>
<servlet-name>mywebservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:conf/dispatcherServlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>mywebservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
2、SSM整合注解开发
(1)建立stu表
(2)编写resources目录下的配置文件
A、jdbc属性配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3366/ssm?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
B、Spring配置文件applicationContext.xml
<context:property-placeholder location="classpath:conf/jdbc.properties" />
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="20" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="configLocation" value="classpath:conf/mybatis.xml" />
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.hcz.dao"/>
bean>
<context:component-scan base-package="com.hcz.service"/>
C、SpringMVC配置文件dispatcherServlet.xml
<context:component-scan base-package="com.hcz.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
bean>
<mvc:annotation-driven/>
D、MyBatis配置文件mybatis.xml
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
settings>
<typeAliases>
<package name="com.hcz.entity"/>
typeAliases>
<mappers>
<package name="com.hcz.dao"/>
mappers>
configuration>
(3)创建实体类Student
public class Student {
private Integer id;
private String name;
private Integer age;
}
(4)Dao接口和sql映射文件
public interface StudentDao {
int insertStudent(Student student);
List<Student> selectStudents();
}
<mapper namespace="com.hcz.dao.StudentDao">
<select id="selectStudents" resultType="Student">
select id,age,name from stu order by id desc
select>
<insert id="insertStudent">
insert into stu(name ,age) values (#{name},#{age})
insert>
mapper>
(5)Service接口和实现类
public interface StudentService {
int addStudent(Student student);
List<Student> findStudents();
}
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public int addStudent(Student student) {
int num = studentDao.insertStudent(student);
return num;
}
@Override
public List<Student> findStudents() {
return studentDao.selectStudents();
}
}
(6)处理器定义
@Controller
@RequestMapping("/student")
public class StudentController {
@Resource
private StudentService service;
@RequestMapping("/addStudent.do")
public ModelAndView addStudent(Student student){
ModelAndView mv = new ModelAndView();
String tips = "注册失败";
int nums = service.addStudent(student);
if(nums > 0){
tips = "学生【"+student.getName()+"】注册成功";
}
mv.addObject("tips",tips);
mv.setViewName("result");
return mv;
}
@RequestMapping("/queryStudent.do")
@ResponseBody
public List<Student> queryStudent(){
List<Student> students = service.findStudents();
return students;
}
}
(7)定义视图(首页面index.jsp)
<body>
<div align="center">
<p>SSM整合例子p>
<img src="images/ssm.jpg"/>
<table>
<tr>
<td><a href="addStudent.jsp"> 注册学生a>td>
tr>
<tr>
<td><a href="listStudent.jsp">浏览学生a>td>
tr>
table>
div>
body>
(8)注册学生页面(addStudent.jsp)
<body>
<div align="center">
<form action="student/addStudent.do" method="post">
<table>
<tr>
<td>姓名:td>
<td><input type="text" name="name">td>
tr>
<tr>
<td>年龄:td>
<td><input type="text" name="age">td>
tr>
<tr>
<td> td>
<td><input type="submit" value="注册">td>
tr>
table>
form>
div>
body>
(9)浏览学生页面(listStudent.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme()+"://"+
request.getServerName()+":"+request.getServerPort()+
request.getContextPath()+"/";
%>
<html>
<head>
<title>浏览学生Ajaxtitle>
<base href="<%=basePath%>"/>