整合SSM框架
整合SSM框架
简单分为三步:
- 整合MyBatis
- 整合Spring
- 整合SpirngMVC
整合完项目目录

数据库表结构

1.整合MyBatis
创建maven项目导入依赖
<!-- junit, 数据库驱动,连接池,servlet, jsp ,mybatis ,mybatis-spring ,spring -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<!-- mysql的驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<!-- 静态资源导入问题 -->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
创建mybatis核心配置文件,编写实体类,编写Mapper,编写service
<?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>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--配置数据源 现在交给了spring去做 -->
<typeAliases>
<package name="com.flyange.pojo"/>
</typeAliases>
<mappers>
<mapper class="com.flyange.dao.LawMapper"/>
</mappers>
</configuration>
package com.flyange.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* lombok 使用
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LawInfo {
private int id;
private String law_title;
private String law_time;
private String law_type;
private String ask_count;
}
package com.flyange.dao;
import com.flyange.pojo.LawInfo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface LawMapper {
List<LawInfo> getLawList();
LawInfo getLawById(@Param("id") int id);
int addLaw(LawInfo lawInfo);
int deleteLaw(@Param("id") int id);
int updateLaw(LawInfo lawInfo);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.flyange.dao.LawMapper">
<select id="getLawList" resultType="LawInfo">
select * from law_info
</select>
<select id="getLawById" resultType="LawInfo">
select * from law_info where id = #{id}
</select>
<insert id="addLaw" parameterType="LawInfo">
insert into law_info(law_title,law_time,law_type,ask_count)
values (#{law_title},#{law_time},#{law_type},#{ask_count})
</insert>
<delete id="deleteLaw" parameterType="int">
delete from law_info where id = #{id}
</delete>
<update id="updateLaw" parameterType="LawInfo">
update law_info
set law_title = #{law_title},law_time = #{law_time},law_type =#{law_type},ask_count = #{ask_count}
where id = #{id};
</update>
</mapper>
package com.flyange.service;
import com.flyange.pojo.LawInfo;
import java.util.List;
public interface LawService {
List<LawInfo> getLawList();
LawInfo getLawById(int id);
int addLaw(LawInfo lawInfo);
int deleteLaw(int id);
int updateLaw(LawInfo lawInfo);
}
package com.flyange.service;
import com.flyange.dao.LawMapper;
import com.flyange.pojo.LawInfo;
import java.util.List;
public class LawServiceImpl implements LawService {
private LawMapper lawMapper;
public void setLawMapper(LawMapper lawMapper) {
this.lawMapper = lawMapper;
}
public List<LawInfo> getLawList() {
return lawMapper.getLawList();
}
public LawInfo getLawById(int id) {
return lawMapper.getLawById(id);
}
public int addLaw(LawInfo lawInfo) {
return lawMapper.addLaw(lawInfo);
}
public int deleteLaw(int id) {
return lawMapper.deleteLaw(id);
}
public int updateLaw(LawInfo lawInfo) {
return lawMapper.updateLaw(lawInfo);
}
}
编写数据库配置文件、
db.driver=com.mysql.jdbc.Driver
#如果使用的是 mysql8.0+ 需要增加一个时区配置 &serverTimezone=Asia/Shanghai
db.url=jdbc:mysql://localhost:3306/nutzfw-open?useSSL=false&useUnicode=true&characterEncoding=utf-8
db.username=root
db.password=root
2.整合spring
整合dao层配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1.关联数据库配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2.数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<!-- 配置初始化大小、最小、最大 -->
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
</bean>
<!-- 3.sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 绑定mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 4.配置dao接口扫描包,动态的实现了dao接口可以注入到Spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 要扫描的dao包 -->
<property name="basePackage" value="com.flyange.dao"/>
</bean>
</beans>
整合service层配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1. 扫描 service下的包 -->
<context:component-scan base-package="com.flyange.service"/>
<!--2. 将所有的业务类注入到Spring,可以通过配置,或者注解实现( @Service ) -->
<bean id="lawServiceImpl" class="com.flyange.service.LawServiceImpl">
<property name="lawMapper" ref="lawMapper"/>
</bean>
<!-- 3.声明式事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 4.AOP事务支持 -->
</beans>
3.整合springmvc
编写controller、 web.xml 、整合spirngmvc配置文件
package com.flyange.controller;
import com.flyange.pojo.LawInfo;
import com.flyange.service.LawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/law")
public class LawController {
@Autowired
@Qualifier("lawServiceImpl")
private LawService lawService;
@RequestMapping("/all")
public String getLaws(Model model){
List<LawInfo> lawList = lawService.getLawList();
model.addAttribute("list",lawList);
return "allLaw";
}
@RequestMapping("/toAdd")
public String toAdd(){
return "add";
}
@RequestMapping("/add")
public String add(LawInfo lawInfo){
lawService.addLaw(lawInfo);
return "redirect:/law/all";
}
@RequestMapping("/toUpdate")
public String toUpdate(int id,Model model){
LawInfo info = lawService.getLawById(id);
model.addAttribute("info",info);
return "update";
}
@RequestMapping("/update")
public String update(LawInfo lawInfo){
int i = lawService.updateLaw(lawInfo);
if(1 > 0){
System.out.println("修改成功");
}
return "redirect:/law/all";
}
@RequestMapping("/delete/{lawId}")
public String update(@PathVariable("lawId") int id){
lawService.deleteLaw(id);
return "redirect:/law/all";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1.注解驱动-->
<mvc:annotation-driven/>
<!--2.静态资源过滤-->
<mvc:default-servlet-handler/>
<!--3.扫描包:contorller-->
<context:component-scan base-package="com.flyange.controller"/>
<!--4.视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
<!-- 启用是为了推迟文件解析,以便捕获文件大小异常 -->
<property name="resolveLazily" value="true"/>
</bean>
</beans>
最后记着把3层的配置文件都引入spring的主配置文件中。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring-dao.xml"/>
<import resource="classpath:spring-service.xml"/>
<import resource="classpath:spring-mvc.xml"/>
</beans>
4.前端的相关页面
index.jsp
allLaw.jsp
add.jsp
update.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
<style type="text/css">
a{
text-decoration: none;
color: black;
}
h3{
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background-color: cornflowerblue;
border-radius: 15px;
}
</style>
</head>
<body>
<h3>
<a href="${pageContext.request.contextPath}/law/all">进入律师页面</a>
</h3>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>案件列表</title>
<style type="text/css">
a{
text-decoration: none;
color: black;
}
.button{
width: 50px;
height: 30px;
background-color: cadetblue;
text-align: center;
line-height: 30px;
display: inline-block;
}
table,table tr th, table tr td {
border:1px solid #1c181e;
}
table {
text-align: center;
border-collapse: collapse;
padding:2px;
}
</style>
</head>
<body>
<div>
<h3>案件列表</h3>
<div class="button">
<a href="${pageContext.request.contextPath}/law/toAdd">添加</a>
</div>
<table>
<thead>
<tr>
<th>案件编号</th>
<th>案件标题</th>
<th>发布时间</th>
<th>访问次数</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="law" items="${list}">
<tr>
<td>${law.id}</td>
<td>${law.law_title}</td>
<td>${law.law_time}</td>
<td>${law.ask_count}</td>
<td>
<div class="button"><a href="${pageContext.request.contextPath}/law/toUpdate?id=${law.id}">修改</a></div> |
<div class="button"><a href="${pageContext.request.contextPath}/law/delete/${law.id}">刪除</a></div>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style type="text/css">
#tj{
width: 50px;
height: 30px;
background-color: cadetblue;
text-align: center;
line-height: 30px;
display: inline-block;
}
</style>
</head>
<body>
<form action="${pageContext.request.contextPath}/law/add" method="post">
<input type="text" id="law_title" name="law_title" required /></br>
<input type="date" id="law_time" name="law_time" required /></br>
<input type="text" id="law_type" name="law_type" required /></br>
<input type="text" id="ask_count" name="ask_count" required /></br>
<input id="tj" type="submit" value="提交">
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style type="text/css">
#tj{
width: 50px;
height: 30px;
background-color: cadetblue;
text-align: center;
line-height: 30px;
display: inline-block;
}
</style>
</head>
<body>
<form action="${pageContext.request.contextPath}/law/update" method="post">
<input type="hidden" name="id" value="${info.id}"/>
<input type="text" id="law_title" name="law_title" value="${info.law_title}" required /></br>
<input type="date" id="law_time" name="law_time" value="${info.law_time}" required /></br>
<input type="text" id="law_type" name="law_type" value="${info.law_type}" required /></br>
<input type="text" id="ask_count" name="ask_count" value="${info.ask_count}" required /></br>
<input id="tj" type="submit" value="提交">
</form>
</body>
</html>
整合完毕,例子中添加了增删改查的方法.效果


浙公网安备 33010602011771号