我是靠谱客的博主 迅速大白,最近开发中收集的这篇文章主要介绍Java学习不走弯路教程(23 整合SSM),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

整合SSM

一. 前言
在前上一章教程中,我们实现了springmvc框架。
本章将在上一章的基础上,进一步扩展程序。

注:
1.本文针对初学Java的同学训练学习思路,请不要太纠结于细节问题。
2.本文旨在达到抛砖引玉的效果,希望大家扩展本例子,以学到更多知识的精髓。

学习本章需要准备的知识:
1.读完本系列教程的前面章节。
2.了解SSM的基本用法。

二. 步入正题
话不多说,大家自己理解,下面步入正题:

我们将SpringMVC,Spring,Mybatis整合到一起。

首先把如下Jar包下载并放到工程的lib目录下:
com.mysql.jdbc_5.1.5.jar
commons-dbcp-1.4.jar
commons-logging-1.2.jar
commons-pool-1.6.jar
mybatis-3.4.6.jar
mybatis-spring-1.3.2.jar
spring-aop-5.1.0.RELEASE.jar
spring-beans-5.1.0.RELEASE.jar
spring-context-5.1.0.RELEASE.jar
spring-core-5.1.0.RELEASE.jar
spring-expression-5.1.0.RELEASE.jar
spring-jdbc-5.1.0.RELEASE.jar
spring-tx-5.1.0.RELEASE.jar
spring-web-5.1.0.RELEASE.jar
spring-webmvc-5.1.0.RELEASE.jar

工程的包结构如下:

1.整合SpringMVC和Spring
我们采用注解的方式声明Service,修改PersonService.java如下:

 1 package vip.java123.fileview.app.service;
 2
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5
 6 import vip.java123.fileview.app.dao.PersonMapper;
 7 import vip.java123.fileview.app.dao.entity.Person;
 8
 9 /**
10  *
11  * @author http://www.java123.vip
12  *
13
*/
14 @Service
15 public class PersonService {
16
17 
@Autowired
18
private PersonMapper personDao;
19
20
public Person getPerson(String personid) {
21
Person person = personDao.selectPerson(personid);
22
return person;
23 
}
24 }

 

然后让Spring扫描我们的service包即可:
app-context.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="vip.java123.fileview.app.web"/>
<context:component-scan base-package="vip.java123.fileview.app.service"/>
<mvc:annotation-driven/>
<import resource="spring-mybatis.xml"/>
</beans>

 

2.整合Spring和Mybatis
我们需要把Mybatis的SessionFactory和Mapper让Spring来识别并进行管理.
首先我们把dao换成Mapper接口,然后让Spring加载Mapper接口即可。
代码如下:

 1 package vip.java123.fileview.app.dao;
 2
 3 import vip.java123.fileview.app.dao.entity.Person;
 4
 5 /**
 6  *
 7  * @author http://www.java123.vip
 8  *
 9
*/
10 public interface PersonMapper {
11
12
public Person selectPerson(String personid);
13 }

PersonMapper.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="vip.java123.fileview.app.dao.PersonMapper">
<select id="selectPerson" resultType="vip.java123.fileview.app.dao.entity.Person">
select * from person where id = #{arg0}
</select>
</mapper>

spring-mybatis.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!-- 配置数据源 -->
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="1qaz2wsx"/>
<!-- 初始化连接大小 -->
<property name="initialSize" value="0" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置mapper.xml -->
<property name="mapperLocations" value="classpath:vip/java123/fileview/app/dao/*.xml" />
</bean>
<!-- 配置Mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="vip.java123.fileview.app.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 配置Spring的事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

 

3.其他文件

其他文件和上一章一样,不需要改动,我把他们贴在下面:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>fileview_web05</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
 1 package vip.java123.fileview.app.web;
 2
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 import org.springframework.web.bind.annotation.RequestParam;
 8 import org.springframework.web.bind.annotation.ResponseBody;
 9
10 import vip.java123.fileview.app.dao.entity.Person;
11 import vip.java123.fileview.app.service.PersonService;
12
13 /**
14  *
15  * @author http://www.java123.vip
16  *
17
*/
18 @Controller
19 public class PersonController {
20
21 
@Autowired
22
private PersonService personService;
23
24
@RequestMapping(value = "/person.do", method = RequestMethod.GET)
25 
@ResponseBody
26
public String query(@RequestParam("personid")String personId) {
27
System.out.println("in query");
28
Person personResult = personService.getPerson(personId);
29
30
StringBuffer result = new StringBuffer();
31
result.append("id:"+personResult.id);
32
result.append("<br/>username:"+personResult.username);
33
result.append("<br/>password:"+personResult.passwd);
34
35
return result.toString();
36 
}
37 }

 person.html

<!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>Insert title here</title>
<script type="text/javascript">
function doQuery(){
document.getElementById("result").innerHTML = "loading...";
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
// XMLHttpRequest.DONE == 4
if (xmlhttp.status == 200) {
document.getElementById("result").innerHTML = xmlhttp.responseText;
}else {
document.getElementById("result").innerHTML = "loading data error.";
}
}
};
var personid = document.getElementById("personid").value;
xmlhttp.open("GET", "person.do?personid="+personid, true);
xmlhttp.send();
}
</script>
</head>
<body>
<form action="/person" method="get">
please input personid:<input id="personid" name="personid"/><input type="button" value="query" onclick="doQuery()"/>
</form>
<div id="result">
</div>
</body>
</html>

 

三. 测试
启动服务器:

 

向服务器请求person.html文件,在浏览器端输入1,点query按钮:

显示查询结果:

 

完整程序请大家从[这里]下载

如有问题,大家来我的网站进行提问。
https://www.java123.vip/qa

版权声明:本教程版权归java123.vip所有,禁止任何形式的转载与引用。

转载于:https://www.cnblogs.com/java123-vip/p/9771122.html

最后

以上就是迅速大白为你收集整理的Java学习不走弯路教程(23 整合SSM)的全部内容,希望文章能够帮你解决Java学习不走弯路教程(23 整合SSM)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部