1、SSM整合开发
SSM 编程,即SpringMVC+Spring+MyBatis整合。就是将MyBatis整合入Spring。因为SpringMVC原本就是Spring的一部分,不用专门整合。
2、搭建SSM开发环境
1、添加依赖和插件:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- jsp 依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
<!--springmvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- 事务的-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--aspectj 依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!--mybatis 和 和 spring 整合的-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--mysql 驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory><!-- 所在的目录-->
<includes><!-- 包括目录下的.properties,.xml 文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
2、配置web.xml:
<!--注册字符过滤器-->
<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>
<!--注册监听器-->
<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>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcherServlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
3、创建实体类:
public class Student
{
private int id;
private String name;
private int age;
private String address;
private String school;
//getter setter toString...
}
4、Jdbc属性配置文件jdbc.properties
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:8080/StudentManage?characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=root
5、Spring配置文件applicationContext.xml
<context:component-scan base-package="com.rg.service"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--数据源-->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--创建SqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="dataSource" ref="myDataSource"/>
</bean>
<!--创建动态代理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.rg.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
6、Springmvc配置文件
<!--声明扫描器-->
<context:component-scan base-package="com.rg.controllers"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--声明注解驱动-->
<mvc:annotation-driven/>
7、mybatis.xml
<configuration>
<settings>
<!--设置mybatis输出日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.rg.entity"/>
</typeAliases>
<!--SQL映射文件的位置-->
<mappers>
<package name="com.rg.dao"/>
</mappers>
</configuration>
8、Dao接口
public interface StudentDao
{
int insertStudent(Student student);
int updateStudent(Student student);
int deleteStudent(int id);
Student selectStudent(int id);
List<Student> selectStudentAll();
}
9、sql映射文件
<mapper namespace="com.rg.dao.StudentDao">
<select id="selectStudent" resultType="Student" parameterType="int">
select id,name,age,address,school from Student where id = #{id}
</select>
<select id="selectStudentAll" resultType="Student">
select id,name,age,address,school from Student
</select>
<insert id="insertStudent" parameterType="Student">
insert into Student(name,age,address,school) values(#{name},#{age},#{address},#{school})
</insert>
<update id="updateStudent" parameterType="Student">
update Student set name = #{name},age = #{age},address=#{address},school=#{school} where id=#{id}
</update>
<delete id="deleteStudent" parameterType="int">
delete from Student where id=#{id}
</delete>
</mapper>
10、Service接口
public interface StudentService
{
int addStu(Student student);
int modifyStu(Student student);
int removeStu(int id);
Student findStuById(int id);
List<Student> findStuAll();
}
11、Service接口实现类
@Service
public class StudentServiceImpl implements StudentService
{
@Resource
private StudentDao studentDao;
@Override
public int addStu(Student student)
{
return studentDao.insertStudent(student);
}
@Override
public int modifyStu(Student student)
{
return studentDao.updateStudent(student);
}
@Override
public int removeStu(int id)
{
return studentDao.deleteStudent(id);
}
@Override
public Student findStuById(int id)
{
return studentDao.selectStudent(id);
}
@Override
public List<Student> findStuAll()
{
return studentDao.selectStudentAll();
}
}
12、处理器定义
@Controller
public class StudentController
{
@Resource
private StudentService studentService;
@RequestMapping(value = "/selectStudents.do", method = RequestMethod.POST)
@ResponseBody
public List<Student> initStudentList()
{
List<Student> students = studentService.findStuAll();
return students;
}
@RequestMapping(value = "/addStu.do", method = RequestMethod.POST)
public ModelAndView addStu(Student student)
{
int result = studentService.addStu(student);
ModelAndView mv = new ModelAndView();
if (result > 0)
{
mv.setViewName("redirect:index.jsp");
}
return mv;
}
}
13、定义视图-首页文件 --- index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme() + "://" +
request.getServerName() + ":" + request.getServerPort() +
request.getContextPath() + "/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>Title</title>
<script src="js/jquery-3.4.1.min.js"></script>
<script>
$(document).ready(function () {
$.ajax({
url: "selectStudents.do",
type: "post",
dataType: "json",
success: function (data) {
$("#stuList").html("");
$.each(data, function (i, n) {
$("#stuList").append("<tr>")
.append("<td>" + n.name + "</td>")
.append("<td>" + n.age + "</td>")
.append("<td>" + n.address + "</td>")
.append("<td>" + n.school + "</td>")
.append("</tr>")
})
}
})
})
</script>
</head>
<body>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>地址</th>
<th>学校</th>
</tr>
</thead>
<tbody id="stuList">
</tbody>
</table>
</body>
</html>
14、添加页面
<form method="post" action="addStu.do">
UserName:<input type="text" name="name">
Age:<input type="text" name="age">
AddRess:<input type="text" name="address">
School:<input type="text" name="school">
<input type="submit" value="提交">
</form>
最后、base标签是否需要用根据情况。