SSM整合

1pom.xml

<properties>
 <!-- 定义属性 -->
	<!-- 设置项目字符集 -->
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<!-- spring版本号 -- 根据自己使用的版本来定>
	<spring.version>4.3.2.RELEASE</spring.version>
</properties>
<dependencies>
	<!-- 配置servlet -->
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>javax.servlet-api</artifactId>
		<version>3.0.1</version>
		<scope>provided</scope>
	</dependency>
	<!-- 配置jsp -->
	<dependency>
		<groupId>javax.servlet.jsp</groupId>
		<artifactId>javax.servlet.jsp-api</artifactId>
		<version>2.2.1</version>
		<scope>provided</scope>
	</dependency>
	<!-- spring web -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-web</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<!-- spring webmvc -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>${spring.version}</version>
	</dependency>	
	<!-- log4j用于查看sql语句 -->
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.12</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>3.4.5</version>
	</dependency>
	<!-- mybatis整合spring时的插件 -->
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis-spring</artifactId>
		<version>1.3.2</version>
	</dependency>
	<!-- druid数据源 -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>1.1.8</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>8.0.11</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --事务>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-tx</artifactId>
		<version>4.3.2.RELEASE</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jdbc</artifactId>
		<version>4.3.2.RELEASE</version>
	</dependency>
	<!-- spring的核心jar包 core context beans  expression-->
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>4.3.2.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>4.3.2.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-beans</artifactId>
		<version>4.3.2.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-expression</artifactId>
		<version>4.3.2.RELEASE</version>
	</dependency>
	<!--使用AspectJ方式注解需要相应的包 --利用注解进行操作>
	<dependency>
		<groupId>org.aspectj</groupId>
		<artifactId>aspectjrt</artifactId>
		<version>1.6.11</version>
	</dependency>
	<!--使用AspectJ方式注解需要相应的包 -->
	<dependency>
		<groupId>org.aspectj</groupId>
		<artifactId>aspectjweaver</artifactId>
		<version>1.6.11</version>
	</dependency>
	<dependency>
		<groupId>jstl</groupId>
		<artifactId>jstl</artifactId>
		<version>1.2</version>
	</dependency>
	<dependency>
		<groupId>taglibs</groupId>
		<artifactId>standard</artifactId>
		<version>1.1.2</version>
	</dependency>
</dependencies>
<build>
	<!-- 配置tomcat插件,web端 -->
	<plugins>
		<plugin>
			<groupId>org.apache.tomcat.maven</groupId>
			<artifactId>tomcat7-maven-plugin</artifactId>
			<configuration>
				<!-- 配置项目的请求路径 -->
				<path>/</path>
				<!-- 配置服务器端口号 -->
				<port>9999</port>
			</configuration>
		</plugin>
	</plugins>
</build>

2.web.xml

<web-app xmlns:xsi="http://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">	
<!-- 在servlet上下文中配置ioc容器文件 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!-- 通过监听器加载ioc容器 -->
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置过滤器 -->
<!-- POST中文乱码过滤器 -->
<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>
<!-- 配置前端控制器 -->
<servlet>
	<servlet-name>DispatcherServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!-- 默认加载springmvc配置文件【servlet-name】-servlet.xml 位置在WEB-INF下 -->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:springmvc.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>	
<servlet-mapping>
	<servlet-name>DispatcherServlet</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>	</web-app>

3.src/main/resources

 **1.	springmvc.xml**
		<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
 http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
 <!-- 启用mvc注解 -->
<mvc:annotation-driven />
<!-- 静态资源 -->
<mvc:default-servlet-handler/>
<!--基于扫描classpath方式  --注解@Controller  @RequestMapper>
<context:component-scan base-package="cn.edu360.web.controller"></context:component-scan>
<!--viewresolver  视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
</bean>
</beans>

**2.   applicationContext-crm.xml**
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 启用classpath扫描 -->
<context:component-scan base-package="cn.edu360"></context:component-scan>	</beans>

**3    .mybatisConfig.xml**
      <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
**4      .db.properties**
driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/crm?serverTimezone=Asia/Shanghai
name=root
password=123456
**5.     log4j.properties**

**6      .applicationContext-mybatis.xml**
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 加载数据库配置文件 -->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- 创建数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
	init-method="init" destroy-method="close">
	<!-- 配置数据库连接基本信息 -->
	<property name="driverClassName" value="${driverClass}" />
	<property name="url" value="${url}" />
	<property name="username" value="${name}" />
	<property name="password" value="${password}" />
	<!-- ******配置数据库连接池相关信息******* -->
	<!-- 配置初始化大小、最小、最大 -->
	<property name="initialSize" value="5" />
	<property name="minIdle" value="2" />
	<property name="maxActive" value="10" />
	<!-- 配置获取连接等待超时的时间 -->
	<property name="maxWait" value="10000" />
	<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
	<property name="timeBetweenEvictionRunsMillis" value="60000" />
	<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
	<property name="minEvictableIdleTimeMillis" value="300000" />
	<property name="testWhileIdle" value="true" />
	<!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
	<property name="testOnBorrow" value="true" />
	<property name="testOnReturn" value="false" />
	<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
	<property name="poolPreparedStatements" value="true" />
	<property name="maxPoolPreparedStatementPerConnectionSize"
		value="20" />
	<!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->
	<property name="defaultAutoCommit" value="true" />
	<!-- 验证连接有效与否的SQL,不同的数据配置不同 -->
	<property name="validationQuery" value="select 1" />
</bean>
<!-- SqlSessionFactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 注入数据源 -->
	<property name="dataSource" ref="dataSource"></property>
	<!-- 加载配置文件 -->
	<property name="configLocation" value="classpath:mybatisConfig.xml"></property>
	<!-- 加载映射文件 -->
	<property name="mapperLocations" value="classpath:cn/edu360/mybatis/entity/*.xml"></property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager"
	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<!-- 注入数据源 -->
	<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用基于注解实现事务管理 -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 扫描dao层接口 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="cn.edu360.mybatis.mapper"></property>
	<!--注入SqlSessionFactory  -->
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
</bean>
</beans>
**     Deptmapper.xml映射文件**
<!-- 查询全部 -->
<select id="selectAllDept" resultType="cn.edu360.crm.entity.Dept">
	select * from dept
</select>

<!-- 传参添加部门信息 -->
<insert id="insertDeptParams" parameterType="cn.edu360.crm.entity.Dept">
	INSERT INTO dept
	VALUES(#{deptno},#{dname},#{loc})
</insert>


<!-- ID查询 -->
<select id="selectDeptById" parameterType="int" resultType="cn.edu360.crm.entity.Dept">
	select * from dept
	where deptno=#{dn}
</select>

<!-- id删除 -->
<delete id="delDeptById" parameterType="int">
	delete from dept where
	deptno=#{dn}
</delete>
<!-- id更新 -->
<update id="updDeptById" parameterType="cn.edu360.crm.entity.Dept">
	update dept set
	dname=#{dname},loc=#{loc} where deptno=#{deptno}
</update>

<!-- 模糊查询 -->
<select id="selectDeptByLike" parameterType="java.lang.String"
	resultType="cn.edu360.crm.entity.Dept">
	SELECT * FROM dept WHERE dname LIKE CONCAT('%',#{keyWord},'%')
</select>

4.src/main/java
entity包中: 实体类 + Mapper.xml映射文件(用于写sql语句CURD)

mapper包中(映射器:相当于之前Dao层的接口)

service.inter包中:与mapper包中的方法保持一致

service.impl包中:声明dao层对象,实现接口方法
其中用到的注解有@Component @Resource/@Autowired
事务注解@Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.NOT_SUPPORTED,readOnly=false,timeout=-1,noRollbackFor=Exception.class)

controller包中:实现CURD
用于连接service层: @Component @Resource/@Autowired
用于与jsp页面跳转: @Controller @RequestMapping("/ ")

**总结整合步骤:**
  • 1.搭建环境,导入maven依赖

  • 2.建表、建实体类

  • 3.创建映射文件

  • 4.创建dao层接口

  • 5.创建mybatis配置文件

  • 6.创建IOC容器整合mybatis
    6.1配置druid数据源 创建db.properties文件
    6.2SqlSessionFactoryBean
    6.3TransactionManager
    6.4启用注解事务
    6.5扫描dao层接口

  • 7.创建业务层接口

  • 8.创建业务层实现类

  • 9.创建ioc容器,管理dao层和Service层的bean

  • 10.给dao层、Service层添加注解管理及实现依赖注入+业务层添加事务

  1. 11.单元测试,测试每个业务方法均能正常运行
  • 12.整合springmvc之创建springmvc配置文件

  • 13.整合springmvc之配置前端控制器

  • 14.整合springmvc之创建页面控制器

  • 15.加载spring的ioc容器

  • .16控制器中实现id查询

    /** ID查询 */

     @RequestMapping("/selectDeptById")
     public ModelAndView selectDeptById(int  deptno) {
     ModelAndView mav=new ModelAndView();
     DeptEntity dept=service.selectDeptById(deptno);
     //把数据放入model中
     mav.addObject("dept", dept);
     mav.setViewName("detail");
     return mav;	}
     11.17查询全部
     	/** 查询全部 */	
     	@RequestMapping("/selectAllDept")public ModelAndView selectAllDept() {
     	ModelAndView mav=new ModelAndView();
     	List<DeptEntity> list=service.selectAllDept();
     		mav.addObject("deptList", list);
     		mav.setViewName("list");
     		return mav;	}
    
<div align="center">
<table>
<tr><td>部门编号</td><td>部门名称</td><td>部门地址</td><td>删除</td></tr>
<c:forEach items="${deptList}" var="dept">
<tr><td>${dept.deptno}</td><td>${dept.dname}</td><td>${dept.loc}</td><td><a href="delDeptById?deptno=${dept.deptno}">删除</a></td></tr>
</c:forEach>
	</table>
</div>
11.18id删除
/** ID删除 */
@RequestMapping("/delDeptById")
public String delDeptById(int deptno) {
	service.delDeptById(deptno);
	return "forward:selectAllDept";
}

11.19模糊查询
/** 名称模糊查询 */

@RequestMapping("/selectDeptByLike")
public ModelAndView selectDeptByLike(String keyWord) {
	ModelAndView mav=new ModelAndView();
	List<DeptEntity> list=service.selectDeptByLike(keyWord);
	mav.addObject("deptList", list);
	mav.setViewName("list");
	return mav;
}

11.20添加部门信息

<form action="insertDeptParams" method="POST" enctype="application/x-www-form-urlencoded">

部门编号:<input type="text" name="deptno"><br>
部门名称:<input type="text" name="dname"><br>
部门地址:<input type="text" name="loc"><br>
<input type="submit"  value="添加部门">
</form>
</div>
/** 添加部门 */
@RequestMapping("/insertDeptParams")
public String insertDeptParams(DeptEntity dept) {
	service.insertDeptParams(dept);
	return "forward:selectAllDept";
}

11.21更新部门
修改list.jsp页面

<div align="center">
<table>
<tr><td>部门编号</td><td>部门名称</td><td>部门地址</td><td>删除</td></tr>
<c:forEach items="${deptList}" var="dept">
<tr><td>${dept.deptno}</td><td><a href="selectDeptById?deptno=${dept.deptno}&type=edit">${dept.dname}</a></td><td>${dept.loc}</td><td><a href="delDeptById?deptno=${dept.deptno}">删除</a></td></tr>
</c:forEach>
	</table>
</div>

Id查询:
/** ID查询 */

@RequestMapping("/selectDeptById")
public ModelAndView selectDeptById(int  deptno,String type) {//type==edit 编辑 type==detail
	ModelAndView mav=new ModelAndView();
	DeptEntity dept=service.selectDeptById(deptno);
	//把数据放入model中
	mav.addObject("dept", dept);
	if("detail".equals(type)) {//id查询
		mav.setViewName("detail");
	}
	if("edit".equals(type)) {//更新
		mav.setViewName("edit");
	}
	return mav;
}

Edit.jsp:

<div align="center">
<form action="updDeptById" method="POST" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="deptno" value="${dept.deptno}"><br>
部门名称:<input type="text" name="dname" value="${dept.dname}"><br>
部门地址:<input type="text" name="loc" value="${dept.loc}"><br>
<input type="submit"  value="修改部门">
</form>
</div>
控制器: /**更新部门信息*/
@RequestMapping("/updDeptById")
public String  updDeptById(DeptEntity dept) {
	service.updDeptById(dept);
	return "forward:selectAllDept";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值