===================================================================================================**
一,整体结构
二,所需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> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <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> </servlet> <servlet-mapping> <servlet-name>controller</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
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"/> <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;"> <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;"> <property name="dataSource" ref="dataSource"></property> </bean> </span> <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> <bean id="studentDao" class="com.mapc.dao.impl.StudentDaoImpl" p:sqlSession-ref="sqlSessionTemplate"/></beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
属性文件:
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode\=true&characterEncoding\=UTF8jdbc.username=rootjdbc.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"> <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"> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
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 + "]"; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
———-dao以及dao.impl
package com.mapc.dao;import java.util.List;import com.mapc.bean.Student;/** * 持久层接口 * @author DC * */public interface StudentDao { /** * 查询所有学生 */ List<Student> queryStudentList();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
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"); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
————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();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
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(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
——–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; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
五,数据表
六,运行测试(利用别的测试手段也得)
0 0
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(2)
- maven+Spring+SpringMVC+Mybatis 整合 xml配置
- SSM简单整合(Mybatis,Spring,SpringMVC,基于注解和xml文件配置)
- spring、springmvc、mybatis整合文件配置详解
- Spring+SpringMVC+MyBatis 整合(一)(Maven配置)
- springMVC+mybatis整合之applicationContext.xml基本配置详解
- springMVC+mybatis整合之applicationContext.xml基本配置详解
- SSM(spring+springMVC+mybatis)整合配置
- spring、springmvc、mybatis整合配置(注解)
- spring+springmvc+druid+oscache+mybatis整合web.xml 配置内容
- SpringMVC+Spring+Mybatis整合配置
- Spring+SpringMVC+Mybatis配置整合
- [spring + springmvc + mybatis] 配置整合
- SpringMVC+Spring+Mybatis整合配置
- Spring+SpringMVC+MyBatis整合配置
- mybatis与spring整合(方式一)
- 64bit windows odbc
- ProtocolBuffer ObjectiveC配置使用
- ADB can't work
- 【腾讯优测干货分享】越用越卡为哪般——如何降低App的待机内存(一)
- javascirpt prototype
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
- 使用axis2解析wsdl生成Webservice客户端代码
- Java反射专题2
- 【游戏渲染】【译】Unity3D Shader 新手教程(2/6) —— 积雪Shader
- LinkedHashMap和HashMap的比较使用
- 【伪】第一次在Xcode中安装CocoaPods并,安装第三方框架AFNetWorking!
- 作用域public,protected,private,以及不写时的区别?
- 2016秋季美团面经_后台研发
- linux常用命令——install