JavaEE:SSM整合(Spring+SpringMVC+MyBatis)

说明:

简称SSM,Spring+SpringMVC+MyBatis

Spring依赖:
spring-core-x.x.x.RELEASE.jar、asm-x.x.jar、cglib-x.x.x.jar、spring-context-x.x.x.RELEASE.jar、spring-beans-x.x.x.RELEASE.jar、spring-expression-x.x.x.RELEASE.jar、spring-tx-x.x.x.RELEASE.jar、spring-aop-x.x.x.RELEASE.jar、spring-aspects-x.x.x.RELEASE.jar、spring-jdbc-x.x.x.RELEASE.jar
AspecjJ依赖jar包:
aspectjweaver-x.x.x.jar、aopalliance-x.x.jar

SpringMVC依赖:
spring-web-x.x.x.RELEASE.jar、spring-webmvc-x.x.x.RELEASE.jar

MyBatis依赖:
mybatis-x.x.x.jar、mybatis-spring-x.x.x.jar、ant-x.x.x.jar、ant-launcher-x.x.x.jar、asm-x.x.jar、cglib-x.x.xx.jar、、javassist-x.x.x-GA.jar、log4j-x.x.x.jar、log4j-api-x.x.x.jar、log4j-core-x.x.x.jar、ognl-x.x.x.jar、slf4j-api-x.x.x.jar、slf4j-log4j12-x.x.x.jar

其他依赖包:
MySql驱动包:mysql-connector-java-x.x.x.jar
Druid依赖(数据源):druid-x.x.x.jar
日志:commons-logging-x.x.jar
json依赖:jackson-annotations-x.x.x.jar、jackson-core-x.x.x.jar、jackson-databind-x.x.x.jar
jsp依赖:jsp-api.jar

一、MyBatis配置:

1.创建mybatis-config.xml,configuration中为空:

<?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>
</configuration>

2.创建dao接口类:

//dao层,操作数据库增删改查
public interface UserMapper {
	public User query(String userNo); // 按userNo查询User
}

3.创建UserMapper.xml,配置dao接口对应的映射:

<?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">
<!-- UserMapper为User的dao接口 -->
<mapper namespace="com.yyh.hkw.dao.UserMapper">
	<!-- 查询User数据 ,parameterType为参数类型,resultType为返回值类型 -->
	<select id="query" parameterType="string" resultType="com.yyh.hkw.domain.User">
		select * from user where userNo=#{userNo}
	</select>
</mapper>

4.创建实体类User.java:

public class User {
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

二、Spring配置:

1.创建jdbc.properties,配置数据库信息:

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC
jdbc.username=root
jdbc.password=root

2.创建spring-config.xml,配置数据源、SqlSessionFactory、Dao动态代理(dao包扫描)、事务管理器、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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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
		http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 一、 配置数据源 -->
	<!-- (1)导入jdbc.properties -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	<!-- (2)配置Druid数据源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driver}" /><!-- 获取jdbc.properties中的值 -->
		<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">
		<!-- 设置数据源,ref值为上面配的数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis-config.xml -->
		<property name="configLocation" value="classpath:mybatis-config.xml" />
	</bean>

	<!-- 三、Dao动态代理配置,dao包扫描,自动创建实例 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.yyh.hkw.dao" />
	</bean>

	<!-- 四、配置事务管理器 -->
	<!-- (1)为事务赋值数据源 -->
	<bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- ref值为上面配的连接池 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- (2),配置通知,以下方法需要被事务管理,tm为上面自定义名 -->
	<tx:advice id="adviceRef" transaction-manager="tm">
		<tx:attributes>
			<!-- 配置相应方法的传播行为 -->
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!--(3)配置切面,使用事务增强service包所有类的所有方法 -->
	<aop:config>
		<!-- adviceRef为上面自定义名,配置表达式,增强service包所有类的所有方法;第1* 表示任意返回值,..表示方法有任意个参数; -->
		<aop:advisor advice-ref="adviceRef" pointcut="execution(* com.yyh.hkw.service.*.*(..))" />
	</aop:config>

	<!-- 五、配置service层包扫描,自动创建实例 -->
	<context:component-scan base-package="com.yyh.hkw.service" />
</beans>

3.配置Tomcat启动时自动加载spring-config.xml文件,web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" metadata-complete="true" version="4.0">
	<!-- 一、spring配置:加载spring-config.xml文件,ContextLoaderListener类会自动加载 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
        <!-- 如果不在src目录下,需要配成classpath*:spring-config.xml -->
		<param-value>classpath:spring-config.xml</param-value>
	</context-param>
	<!-- spring配置:启动服务器时调用contextInitialized,停止服务器时调用contextDestroyed。 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	...
</web-app>

4.创建service层类UserService.java,调用dao接口实现增删改查:

//service层,操作dao层增删改查
@Service
public class UserService {
	@Autowired //对象类型注入,自动实例化
	private UserMapper mUserDao;
	// 按userNo查询User
	public User getUserDetail(String userNo) {
		return mUserDao.query(userNo);
	}
}

三、SpringMVC配置:

1.创建springmvc-config.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans 
	https://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 一、配置包扫描,自动实例化control包下所有类 -->
	<context:component-scan base-package="com.yyh.hkw.control" />

	<!-- 二、配置注解驱动,等同于配置映射器+适配器,处理请求路径到方法的调用 -->
	<mvc:annotation-driven />

	<!-- 三、配置路径前/后缀,在跳转时可省略前/后缀 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

2.配置拦截.action的请求路径,自动加载springmvc-config.xml文件,web.xml中:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" metadata-complete="true" version="4.0">
	...
	<!-- 二、springmvc配置:配置拦截.action的请求路径 -->
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 加载springmvc-config.xml文件 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc-config.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
</web-app>

3.创建control层类UserControl.java,调用service层实现增删改查:

//@Controller配置当前类为页面求处理类
@Controller
public class UserControl {
	@Autowired // 对象类型注入,自动实例化
	private UserService mUserService;

	@RequestMapping("userDetail")
	public String userDetail(String userNo, Model model) {
		System.out.println("userDetail userNo: " + userNo);
		User user = mUserService.getUserDetail(userNo);
		model.addAttribute("user", user); // 将值传给页面
		return "user_detail"; // 跳user_detail.jsp界面
	}
}

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值