每天分享技术栈,开发工具等二话不说开干
SSM = Spring+SpringMVC+Myatis
SSM = Spring+SpringMVC+Mybatis= (Spring+Mybatis)+SpringMVC
先整合Spring+Mybatis
然后再整合SpringMVC
(具体pom坐标和其他方法在上一篇传送门走你)
配置监听器实现启动服务创建容器
<!-- 配置spring提供的监听器,用于启动服务时加载容器 。 该间监听器只能加载WEB-INF目录中名称为applicationContext.xml的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 手动指定spring配置文件位置 --> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
保证MyBatis框架在web工程中独立运行第一步:编写AccountDao映射配置文件
<?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.dao.IAccountDao">
<!-- 查询所有账户 -->
<select id="findAll" resultType="com.domain.Account">
select * from account </select> <!-- 新增账户 -->
<insert id="save" parameterType="com.domain.Account">
insert into account(name,money) values(#{name},#{money}); </insert>
</mapper>
第二步:编写SqlMapConfig配置文件
<?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>
<properties resource="jdbcConfig.properties"></properties>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="pooled">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/dao/AccountDao.xml"/>
</mappers>
</configuration>
properties文件中的内容:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=1234
第三步:测试运行结果
public class Test02MyBatis {
/**
* 测试保存 * @param args * @throws Exception
*/
@Test
public void testSave() throws Exception {
Account account = new Account();
account.setName("test");
account.setMoney(5000f);
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession session = factory.openSession();
IAccountDao aDao = session.getMapper(IAccountDao.class);
aDao.save(account);
session.commit();
session.close();
in.close();
}
/**
* 测试查询 *
*/
@Test
public void testFindAll() throws Exception {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession session = factory.openSession();
IAccountDao aDao = session.getMapper(IAccountDao.class);
List<Account> list = aDao.findAll();
System.out.println(list);
session.close();
in.close();
}
}
整合Spring和MyBatis整合思路:把mybatis配置文件(SqlMapConfig.xml)中内容配置到spring配置文件中 同时,把mybatis配置文件的内容清掉。
<?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>
注意:由于我们使用的是代理Dao的模式,Dao具体实现类由MyBatis使用代理方式创建,所以此时mybatis配置文件不能删。当我们整合spring和mybatis时,mybatis创建的Mapper.xml文件名必须和Dao接口文件名一致
第一步:Spring接管MyBatis的Session工厂
<!-- 加载配置文件 -->
</bean>
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:jdbcConfig.properties"/>
<!-- 配置MyBatis的Session工厂 -->
<bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
第二步:配置自动扫描所有Mapper接口和文件
<!-- 配置Mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao"/>
</bean>
第三步:配置spring的事务
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="pt1"/>
<!-- 建立通知和切入点表达式的关系 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>
第四步:测试整合结果
/**
* 测试spring整合mybatis
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class Test03SpringMabatis {
@Autowired
private IAccountService accountService;
@Test
public void testFindAll() {
List list = accountService.findAllAccount();
System.out.println(list);
}
@Test
public void testSave() {
Account account = new Account();
account.setName("测试账号");
account.setMoney(1234f);
accountService.saveAccount(account);
}
}
测试SSM整合结果编写测试jsp请求发起页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主页</title></head>
<body><a href="account/findAllAccount">访问查询账户</a>
<hr/>
<form action="account/saveAccount" method="post"> 账户名称:<input type="text" name="name"/><br/> 账户金额:<input type="text"
name="money"><br/>
<input type="submit" value="保存"/></form>
</body>
</html> 响应结果页面: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>账户的列表页面</title>
</head>
<body>
<table border="1" width="300px">
<tr>
<th>编号</th>
<th>账户名称</th>
<th>账户金额</th>
</tr>
<c:forEach items="${accounts}" var="account" varStatus="vs">
<tr>
<td>${vs.count}</td>
<td>${account.name }</td>
<td>${account.money }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
修改控制器中的方法
/**
* 账户的控制器
*/
@Controller("accountController")
@RequestMapping("/account")
public class AccountController {
@Autowired
private IAccountService accountService;
/**
* 查询所有账户
*/
@RequestMapping("/findAllAccount")
public ModelAndView findAllAccount() {
List<Account> accounts = accountService.findAllAccount();
ModelAndView mv = new ModelAndView();
mv.addObject("accounts", accounts);
mv.setViewName("accountlist");
return mv;
}
/**
* 保存账户
*/
@RequestMapping("/saveAccount")
public String saveAccount(Account account) {
accountService.saveAccount(account);
return "redirect:findAllAccount";
}
}
测试运行结果
Spring框架中的SpringMVC案例及小点解释 (一)2020-11-11
究竟怎么用Restful风格编代码必看这篇。(二)2020-11-12
Spring 自定义注解玩法大全,从入门到…2020-11-13
中招了?最流行的 RESTful API 要怎么设计?2020-11-14
14 个 Spring MVC 顶级技巧,随时用随时爽,一直用一直爽2020-11-16
自定义拦截器的使用和SSM整合(每步详细步骤)2020-11-17
END
如果看到这里,说明你喜欢这篇文章,请 转发、点赞。同时 本公众号可以第一时间接受到博文推送。