- ssm整合案例
- 一、新建maven工程
- 二、添加依赖
- 三、配置文件
- db.properties
- SqlMapConfig.xml
- applicationContext-dao.xml
- log4j
- 四、工程结构
- 五、spring业务层整合
- applicationContext-tx.xml
- 六、整合SpringMVC
- springmvc.xml
- 七、写界面测试
- add.jsp
- index.jsp
如果有如下报错
处理方式
右键项目名,如图
报错消失,新建完成
插入一个其他可能会遇到的问题,web.xml文件的web-app那里报错
如图:(没报错的话可以直接跳到下一步添加依赖)
解决方法
二、添加依赖再pom.xml文件中添加要用到的依赖
下面是我用的,也可以自行选择其他版本
<!-- 统一管理spring jar包的版本 -->
<properties>
<org.springframework.version>5.2.6.RELEASE</org.springframework.version>
</properties>
<dependencies>
<!-- spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- aop的依赖: aspectj -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<!-- spring 整合junit 测试依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- 数据源产品: dbcp2依赖 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- 数据库驱动: mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- log4j的依赖 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<!-- mybatis的依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- spring整合 mybatis的依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
</dependencies>
添加好后ctrl+s保存,就会自动下载了,如图
如果没有添加成功,按下面的步骤 *** 作
添加完成
三、配置文件在src/main/resources右键,新建配置文件
配置文件内容如下:
db.properties(我这里是mysql8,mysql5需要另行配置)
(数据库名,账号,密码改成自己电脑上的)
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/iflytek02?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
username=root
password=111
SqlMapConfig.xml
<?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>
<!-- 别名配置 -->
<typeAliases>
<!-- mapper的xml文件中用到com.iflytek.pojo包下的所有的类,直接用类名作为别名 -->
<!-- 可以根据自己想要的包名配置、修改 -->
<package name="com.iflytek.pojo"/>
</typeAliases>
</configuration>
applicationContext-dao.xml
(实际上配的就是spring的IOC的这部分功能)
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 加载数据库连接的属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置dbcp2的数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close"
p:driverClassName="${driver}"
p:url="${url}"
p:username="${username}"
p:password="${password}"/>
<!--SqlSessionFactory的配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 配置mybatis的配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean>
<!--mapper的扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 自动生成com.iflytek.mapper下所有接口实现类的对象 -->
<property name="basePackage" value="com.iflytek.mapper"/>
</bean>
</beans>
log4j
(没啥说的,直接复制贴上去完事)
# Global logging configuration
#\u5728\u5f00\u53d1\u73af\u5883\u4e0b\u65e5\u5fd7\u7ea7\u522b\u8981\u8bbe\u7f6e\u6210DEBUG\uff0c\u751f\u4ea7\u73af\u5883\u8bbe\u7f6e\u6210info\u6216error
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
applicationContext-tx.xml配置文件在【五、spring业务层整合】里
springmvc.xml配置文件在【六、整合springmvc】里
四、工程结构
根据自己需要用到的表,编写dao层代码
我用到的表:
编写dao层代码:
package com.iflytek.pojo;
public class Dept {
private Integer deptno;
private String dname;
private String loc;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
新建接口,写增删改查方法:
(这里就用增来举例了)
package com.iflytek.mapper;
import com.iflytek.pojo.Dept;
public interface DeptMapper {
int addDept(Dept dept);
// int updateDept(Dept dept);
//
// int deleteDept(Dept dept);
//
// int findDept(Dept dept);
}
DeptMapper.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.iflytek.mapper.DeptMapper">
<insert id="addDept" parameterType="Dept">
insert into dept(deptno,dname,loc) values(#{deptno},#{dname},#{loc})
</insert>
</mapper>
五、spring业务层整合
DeptService接口
package com.iflytek.service;
import com.iflytek.pojo.Dept;
public interface DeptService {
/* 添加部门 */
int add(Dept dept) throws Exception;
}
DeptServiceImpl实现类
package com.iflytek.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iflytek.mapper.DeptMapper;
import com.iflytek.pojo.Dept;
import com.iflytek.service.DeptService;
@Service("deptService")
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public int add(Dept dept) throws Exception {
return deptMapper.addDept(dept);
}
}
applicationContext-tx.xml
声明式事务的配置applicationContext-tx.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
<!-- 配置事务的通知器 -->
<tx:advice id="txAdvice" transaction-manager="txManager" >
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="find*" propagation="SUPPORTS"/>
<tx:method name="select*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!-- 切点表达式 -->
<aop:config>
<aop:pointcut expression="execution(* com.iflytek.service.*.*(..))" id="serviceMethod"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
</aop:config>
</beans>
六、整合SpringMVC
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://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">
<!-- 开启springmvc的注解驱动 -->
<mvc:annotation-driven />
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.iflytek "/>
</beans>
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>dept</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置springmvc的前端控制器DispatcherServlet -->
<servlet>
<servlet-name>springmvc01</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc01</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 将spring的dao层和事务transection的配置加入容器管理中 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 配置监听器,监听容器范围内各个bean的变化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Controller代码
package com.iflytek.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.iflytek.pojo.Dept;
import com.iflytek.service.DeptService;
@Controller
public class DeptController {
@Autowired
private DeptService deptService;
@RequestMapping("add.do")
public ModelAndView add(Dept dept) throws Exception {
ModelAndView mv = new ModelAndView();
int i = deptService.add(dept);
if(i==1) {
mv.setViewName("index.jsp");
} else {
mv.setViewName("add.jsp");
}
return mv;
}
}
七、写界面测试
写界面
新建jsp文件,如果出现如下错误,如图
处理方法:
右键项目,build path
报错消失
add.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加部门</title>
</head>
<body>
<form action="add.do" method="post">
部门编号 <input type="text" name="deptno"><br>
部门名称 <input type="text" name="dname"><br>
所在城市 <input type="text" name="loc"><br>
<input type="submit" value="添加部门">
</form>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
success!
</body>
</html>
启动服务器
添加部门
刷新数据库
添加成功!!!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)