spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
**这个整合。仅仅是最主要的整合,而且是xml配置文件的方式之中的一个,即当中的mybatis是採用非mapper接口的方式。(第二遍採用mapper接口方式。第三遍採用注解的方式;第四篇採用注解基于maven的方式)。记录在这里,以免下次忘记时留作备用。
===================================================================================================**
一。总体结构
二,所需jar包:
实质上并不须要所有导入,这里为了方便就所有导入啦。
(1)spring4所有jar包
(2)mybatis所有jar包
(3)mybatis-spring.jar整合须要这个包,如今spring官方已经不提供整合的jar,所以须要另外下载mybatis官方提供的整合jar。
(4)这里使用数据源dbcp.jar,依赖pool.jar,因此也须要导进来。
以及数据库连接驱动jar也须要。
三,整合spring。springmvc和mybatis配置
<?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>zh2demo</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>
<!-- 上下文參数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:com/mapc/conf/application-dao.xml,
classpath:com/mapc/conf/application-service.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>
classpath:com/mapc/conf/log4j.properties
</param-value>
</context-param>
<!-- 配置log4j日志监听器,开启日志记录,容器须要 -->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!-- 配置context上下文监听器,创建root容器,和web容器集成在一起 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置springmvc控制器,会创建web上下文容器。而且会设置root上下文容器为此容器的父容器 -->
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:com/mapc/conf/application-controller.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup><!-- web容器启动时就创建该context容器 -->
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 利用spring提供的编码控制过滤器 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
root父容器所需的配置文件:
1。application-dao.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 引入资源文件 -->
<context:property-placeholder file-encoding="utf8" location="classpath:com/mapc/conf/jdbc.properties"/>
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}"/>
<span style="color:#ff0000;">
<!-- 创建SqlSessionFactory。同一时候指定数据源和mybatis配置文件。
特别注意:须要引入mybatis-spring.jar-->
<bean id="sqlSessionFactory" class="</span><span style="color:#000099;">org.mybatis.spring.SqlSessionFactoryBean</span><span style="color:#ff0000;">">
</span><span style="color:#000099;"><property name="configLocation" value="classpath:com/mapc/conf/mybatisconf/Configuration.xml"/> </span><span style="color:#ff0000;"> </span><span style="color: rgb(255, 0, 0); white-space: pre;"> </span><span style="color:#ff0000;"><!--这里提供了集成持久层框架mybatis的集成点-->
<property name="dataSource" ref="dataSource"></property>
</bean>
</span><!-- 配置Session模板类 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
<!-- 配置增强通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="query*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置共享事务 -->
<aop:config expose-proxy="true">
<!-- 配置切点 -->
<aop:pointcut expression="execution(* com.mapc.service..*.*(..))" id="txPointcut"/>
<!-- 配置切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<!-- 装配StudentDao -->
<bean id="studentDao" class="com.mapc.dao.impl.StudentDaoImpl" p:sqlSession-ref="sqlSessionTemplate"/>
</beans>
属性文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode\=true&characterEncoding\=UTF8
jdbc.username=root
jdbc.password=root
2,application-service.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 装配StudentService -->
<bean id="studentService" class="com.mapc.service.impl.StudentServiceImpl"
p:studentDao-ref="studentDao"/>
</beans>
3,其它,这里视详细需求。
web层子容器的配置文件:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 处理器映射器:这样的方式,从url映射到详细的控制器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 视图解析器,配置ContentNegotiatingViewResolver更通用 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/pages/" p:suffix=".jsp"/>
<!-- 静态资源的訪问控制 -->
<mvc:resources location="/resources/" mapping="/resources/**"/>
<!-- 处理器 -->
<bean id="/queryStudentList.do" class="com.mapc.controller.StudentController"
p:studentService-ref="studentService"/>
</beans>
mybatis配置文件Configuration.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>
<mappers>
<mapper resource="com/mapc/conf/mybatisconf/Student.xml"/>
</mappers>
</configuration>
mybatis Mapper的配置文件:
<?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="Student">
<resultMap type="com.mapc.bean.Student" id="studentResultMapper">
<id column="ID" jdbcType="VARCHAR" property="id"/>
<result column="NAME" jdbcType="VARCHAR" property="name"/>
<result column="AGE" jdbcType="INTEGER" property="age"/>
</resultMap>
<select id="queryStudentList" resultMap="studentResultMapper">
select ID,NAME,AGE from STUDENT
</select>
</mapper>
四,java源代码
———-bean
package com.mapc.bean;
/**
* POJO
* @author DC
*
*/
public class Student {
/**
* 学号
*/
private String id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
———-dao以及dao.impl
package com.mapc.dao;
import java.util.List;
import com.mapc.bean.Student;
/**
* 持久层接口
* @author DC
*
*/
public interface StudentDao {
/**
* 查询所有学生
*/
List<Student> queryStudentList();
}
package com.mapc.dao.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import com.mapc.bean.Student;
import com.mapc.dao.StudentDao;
/**
* 持久层详细实现类之中的一个
* @author DC
*
*/
public class StudentDaoImpl implements StudentDao {
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSession(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public List<Student> queryStudentList() {
return sqlSessionTemplate.selectList("Student.queryStudentList");
}
}
————service和service.impl
package com.mapc.service;
import java.util.List;
import com.mapc.bean.Student;
import com.mapc.dao.StudentDao;
/**
* 学生相关的业务接口
* @author DC
*
*/
public interface StudentService {
/**
* 查询学生信息的业务接口方法
*/
public List<Student> queryStudentList();
}
package com.mapc.service.impl;
import java.util.List;
import com.mapc.bean.Student;
import com.mapc.dao.StudentDao;
import com.mapc.service.StudentService;
/**
* 学生业务的详细实现类
* @author DC
*
*/
public class StudentServiceImpl implements StudentService {
/**
* 注入StudentDao
*/
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public List<Student> queryStudentList() {
return studentDao.queryStudentList();
}
}
——–controller页面处理器
package com.mapc.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.mapc.bean.Student;
import com.mapc.service.StudentService;
/**
* 学生控制类
* @author DC
*
*/
public class StudentController implements Controller {
/**
* 注入学生业务类
*/
private StudentService studentService;
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
@Override
public ModelAndView handleRequest(HttpServletRequest req,
HttpServletResponse resp) throws Exception {
ModelAndView mv=new ModelAndView();
List<Student> list=studentService.queryStudentList();
mv.addObject("studentList",list);
System.out.println("----------------:"+list.toString());
mv.setViewName("showStudentList");
return mv;
}
}
五,数据表
六。执行測试(利用别的測试手段也得)
菜鸟首次整合,尽管非常easy,在第一次整合的时候还是出现各种难以预料的错误的。
为了更加清晰这里面的情况,整合了第二次,还是实践可以验证整理。
多实践,多思考,谨记!
spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)的更多相关文章
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM
写在前面的话 承接前文<Spring+SpringMVC+MyBatis+easyUI整合基础篇(五)讲一下maven>,本篇所讲述的是如何使用maven与原ssm项目整合,使得一个普 ...
- spring+springMVC+mybatis简单整合
spring+springMVC+mybatis简单整合, springMVC框架是spring的子项目,所以框架的整合方式为,spring+Mybatis或springMVC+mybatis. 三大 ...
- SSM(Spring,SpringMVC,Mybatis)框架整合项目
快速上手SSM(Spring,SpringMVC,Mybatis)框架整合项目 环境要求: IDEA MySQL 8.0.25 Tomcat 9 Maven 3.6 数据库环境: 创建一个存放书籍数据 ...
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(十一)SVN服务器进阶
日常啰嗦 上一篇文章<Spring+SpringMVC+MyBatis+easyUI整合基础篇(十)SVN搭建>简单的讲了一下SVN服务器的搭建,并没有详细的介绍配置文件及一些复杂的功能, ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(二)Log4j讲解与整合
日常啰嗦 上一篇文章主要讲述了一下syso和Log间的一些区别与比较,重点是在项目的日志功能上,因此,承接前文<Spring+SpringMVC+MyBatis+easyUI整合优化篇(一)Sy ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(四)单元测试实例
日常啰嗦 前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合优化篇(三)代码测试>讲了不为和不能两个状态,针对不为,只能自己调整心态了,而对于不能,本文会结合一 ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(五)结合MockMvc进行服务端的单元测试
日常啰嗦 承接前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合优化篇(四)单元测试实例>,已经讲解了dao层和service层的单元测试,还有控制器这层也不能 ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(七)图片上传功能
日常啰嗦 前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合优化篇(六)easyUI与富文本编辑器UEditor整合>讲了富文本编辑器UEditor的整合与使用 ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(十三)数据层优化-表规范、索引优化
本文提要 最近写的几篇文章都是关于数据层优化方面的,这几天也在想还有哪些地方可以优化改进,结合日志和项目代码发现,关于数据层的优化,还是有几个方面可以继续修改的,代码方面,整合了druid数据源也开启 ...
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(二)RESTful API实战笔记(接口设计及Java后端实现)
写在前面的话 原计划这部分代码的更新也是上传到ssm-demo仓库中,因为如下原因并没有这么做: 有些使用了该项目的朋友建议重新创建一个仓库,因为原来仓库中的项目太多,结构多少有些乱糟糟的. 而且这次 ...
随机推荐
- 重构手法之Replace Temp with Query(以查询取代临时变量)
返回总目录 6.4Replace Temp with Query(以查询取代临时变量) 概要 你的程序以一个临时变量保存某一表达式的运算结果. 将这个表达式提炼到一个独立函数中.将这个临时变量的所有引 ...
- Git命令补全配置
Git命令补全功能 1.下载下面的文件 https://github.com/sguo421/code/blob/master/git-completion.bash 2.放倒HOME目录下,设置为隐 ...
- 【python】python的正则表达式 re
ps:本文摘自互联网,觉得结构很好,讲的也很清晰.记下,备查. 延伸阅读:python的 内建函数 和 subprocess .此文是本系列的第三篇文章了,和之前一样,内容出自官方文档,但是会有自己的 ...
- [转]Oracle 索引质量分析
http://blog.csdn.net/leshami/article/details/23687137 索引质量的高低对数据库整体性能有着直接的影响.良好高质量的索引使得数据库性能得以数量级别的提 ...
- netty 入门(一)
netty Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序.更确切的讲是一个组件,没有那么复杂. 例子 一 Discard服务器端 我们 ...
- 【架构研习】欲善其事先利其器-Robot Framework实战演练之框架的选择
(原创文章,转载请注明出处.) 之前有提到过,自己曾基于公司业务系统从无到有码过一套测试框架,但由于开发时的思想同时受限于公司业务及框架的适用性上,导致最终虽然框架可完美支持业务,但在易用性.兼容性及 ...
- Kaggle初入门
今天成功的进驻kaggle社区了! 所以以后就要跟kaggle上面的各位一起学习啦! 今天十分成功的在tensorflow的环境里面装了一堆库--什么seaborn啊pandas啊都一次过 然后--并 ...
- 使用vue2.x+webpack+vuex+sass+axios+elementUI等快速搭建前端项目框架
一.本文将分享如何快速搭起基于webpack+vue的前端项目框架,利用vue的自己的脚手架工具vue-cli搭建起基本的环境配置,再通过npm包管理工具引入相应的依赖来完善项目的各种依赖框架.下面是 ...
- 2017上半年技术文章集合【Android】—184篇文章分类汇总
地址: http://blog.csdn.net/androidstarjack/article/details/77923753 声明 | 本文是于亚豪 原创 终端研发部 前言: 2017年已经过大 ...
- 75、django之ORM补充
本篇导航: QuerySet 中介模型 查询优化 一.QuerySet 1.可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. > ...