spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含13527字,纯文字阅读大概需要20分钟。
内容图文
**这个整合,只是最基本的整合,并且是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-appxmlns: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"?>
<beansxmlns="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-placeholderfile-encoding="utf8"location="classpath:com/mapc/conf/jdbc.properties"/>
<!-- 配置dbcp数据源 -->
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"p:driverClassName="${jdbc.driver}"p:url="${jdbc.url}"p:username="${jdbc.username}"p:password="${jdbc.password}"/>
<spanstyle="color:#ff0000;">
<!-- 创建SqlSessionFactory,同时指定数据源和mybatis配置文件。特别注意:需要引入mybatis-spring.jar-->
<beanid="sqlSessionFactory"class="</span><span hljs-value">color:#000099;">org.mybatis.spring.SqlSessionFactoryBean</span><spanstyle="color:#ff0000;">">
</span><spanstyle="color:#000099;"><propertyname="configLocation"value="classpath:com/mapc/conf/mybatisconf/Configuration.xml"/></span><spanstyle="color:#ff0000;"></span><spanstyle="color: rgb(255, 0, 0); white-space: pre;"></span><spanstyle="color:#ff0000;"><!--这里提供了集成持久层框架mybatis的集成点--><propertyname="dataSource"ref="dataSource"></property></bean></span><!-- 配置Session模板类 --><beanid="sqlSessionTemplate"class="org.mybatis.spring.SqlSessionTemplate"><constructor-argindex="0"ref="sqlSessionFactory"/></bean><!-- 配置事务管理器 --><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"p:dataSource-ref="dataSource"/><!-- 配置增强通知 --><tx:adviceid="txAdvice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="query*"propagation="REQUIRED"/><tx:methodname="get*"propagation="REQUIRED"/><tx:methodname="add*"propagation="REQUIRED"/><tx:methodname="*"read-only="true"/></tx:attributes></tx:advice><!-- 配置共享事务 --><aop:configexpose-proxy="true"><!-- 配置切点 --><aop:pointcutexpression="execution(* com.mapc.service..*.*(..))"id="txPointcut"/><!-- 配置切面 --><aop:advisoradvice-ref="txAdvice"pointcut-ref="txPointcut"/></aop:config><!-- 装配StudentDao --><beanid="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 -->
<beanid="studentService"class="com.mapc.service.impl.StudentServiceImpl"p:studentDao-ref="studentDao"/>
</beans>
3,其他,这里视具体需求。
web层子容器的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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映射到具体的控制器 -->
<beanclass="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 处理器适配器 -->
<beanclass="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 视图解析器,配置ContentNegotiatingViewResolver更通用 -->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"p:viewClass="org.springframework.web.servlet.view.JstlView"p:prefix="/WEB-INF/pages/"p:suffix=".jsp"/>
<!-- 静态资源的访问控制 -->
<mvc:resourceslocation="/resources/"mapping="/resources/**"/>
<!-- 处理器 -->
<beanid="/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>
<mapperresource="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">
<mappernamespace="Student">
<resultMaptype="com.mapc.bean.Student"id="studentResultMapper">
<idcolumn="ID"jdbcType="VARCHAR"property="id"/>
<resultcolumn="NAME"jdbcType="VARCHAR"property="name"/>
<resultcolumn="AGE"jdbcType="INTEGER"property="age"/>
</resultMap>
<selectid="queryStudentList"resultMap="studentResultMapper">
select ID,NAME,AGE from STUDENT
</select></mapper>
四,java源码
———-bean
package com.mapc.bean;
/**
* POJO
* @author DC
*
*/publicclassStudent {/**
* 学号
*/private String id;
/**
* 姓名
*/private String name;
/**
* 年龄
*/privateint age;
public String getId() {
return id;
}
publicvoidsetId(String id) {
this.id = id;
}
public String getName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
publicintgetAge() {
return age;
}
publicvoidsetAge(int age) {
this.age = age;
}
@Overridepublic String toString() {
return"Student [, name=" + name + ", age=" + age + "]";
}
}
———-dao以及dao.impl
package com.mapc.dao;
import java.util.List;
import com.mapc.bean.Student;
/**
* 持久层接口
* @author DC
*
*/publicinterfaceStudentDao {/**
* 查询所有学生
*/
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
*
*/publicclassStudentDaoImplimplementsStudentDao {private SqlSessionTemplate sqlSessionTemplate;
publicvoidsetSqlSession(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Overridepublic 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
*
*/publicinterfaceStudentService {/**
* 查询学生信息的业务接口方法
*/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
*
*/publicclassStudentServiceImplimplementsStudentService {/**
* 注入StudentDao
*/private StudentDao studentDao;
publicvoidsetStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Overridepublic 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
*
*/publicclassStudentControllerimplementsController {/**
* 注入学生业务类
*/private StudentService studentService;
publicvoidsetStudentService(StudentService studentService) {
this.studentService = studentService;
}
@Overridepublic 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;
}
}
五,数据表
六,运行测试(利用别的测试手段也得)
菜鸟首次整合,虽然很简单,在第一次整合的时候还是出现各种难以预料的错误的。为了更加清晰这里面的情况,整合了第二次,还是实践能够验证整理。多实践,多思考,谨记!
原文:http://blog.csdn.net/duchao123duchao/article/details/51176673
内容总结
以上是互联网集市为您收集整理的spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)全部内容,希望文章能够帮你解决spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。