简介

SSH框架很强大,适合大型项目开发。但学无止境,多学会一门框架组合开发会让自己增值许多。 
SSM框架小巧精致,适合中小型项目快速开发,对于新手来说也是简单上手的。在SSM框架搭建之前,我们先学习组合Spring+MyBatis,下面开始简单实例。


实例

第一步——导包

Spring框架包及其依赖包+MyBatis框架包及其依赖包+EhCache架包+C3P0架包+MySql驱动包

本实例架包目录如下: 

实例项目结构如下: 

第二步——各种配置文件

SpringMVC配置文件(ApplicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <context:component-scan base-package="cn.pwc" /> <bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_pwc" />
<property name="user" value="pwc" />
<property name="password" value="123456" />
<property name="maxPoolSize" value="20" />
<property name="minPoolSize" value="1" />
<property name="initialPoolSize" value="3" />
<property name="maxIdleTime" value="60" />
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.pwc.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>

1.配置C3P0连接池bean和SqlSessionFactory工厂类bean 
2.该实例使用Mapper代理,因此需配置MapperScannerConfigurer的bean 
3.该bean的basePackage属性值为自动扫描的Mapper.xml的所在包 
4.扫描到的Mapper会自动实例化装载,实例的bean名为第一个字母为小写的Mapper接口名,例如该实例接口为UserMapper.java,实例化bean名为userMapper。

MyBatis配置文件(SqlMapConfig.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>
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
<mappers>
<package name="cn.pwc"/>
</mappers>
</configuration>

因为Spring管理各个层,DAO层配置,连接池等都交给Spring配置,因此MyBatis配置只需写简单全局设置和Mapper.xml自动扫描配置。 
这里全局配置打开了二级缓存,即cacheEnabled设置为true。

MyBatis的Mapper配置文件(UserMapper.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="cn.pwc.dao.UserMapper">
<cache type="org.mybatis.caches.ehcache.EhcacheCache" />
<select id="findById" parameterType="int" resultType="cn.pwc.pojo.User">
SELECT * FROM user WHERE id=#{id}
</select>
<select id="findByAge" parameterType="int" resultType="cn.pwc.pojo.User">
SELECT * FROM user WHERE age=#{age}
</select>
<delete id="deleteById" parameterType="int">
DELETE FROM user WHERE id=#{id}
</delete>
<insert id="insert" parameterType="cn.pwc.pojo.User">
INSERT INTO user(name,age) VALUES(#{name},#{age})
</insert>
</mapper>

该ORM映射Mapper文件开启二级缓存,并指定为EhCache交给其管理

EhCache配置文件(ehcache.xml)

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="F:\cache_test" />
<defaultCache eternal="false" maxElementsInMemory="1000"
timeToIdleSeconds="20" timeToLiveSeconds="20" overflowToDisk="false"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="20" memoryStoreEvictionPolicy="LRU" />
</ehcache>

这里特别要注意overflowToDisk属性值,若为true,那么缓存的pojo类需继承Serializable接口

Log4j配置文件(log4j.properties)

log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n

第三步——测试

测试类如下:

package cn.pwc.test;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.pwc.dao.UserMapper;
import cn.pwc.pojo.User; public class Test extends SqlSessionDaoSupport{ public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserMapper mapper=(UserMapper) context.getBean("userMapper");
User user = mapper.findById(1);
System.out.println(user.toString());
User user2 = mapper.findById(1);
System.out.println(user2.toString());
User user3 = mapper.findById(1);
System.out.println(user3.toString());
User user4 = mapper.findById(1);
System.out.println(user4.toString());
context.close(); } }

SpringMVC笔记——Spring+MyBatis组合开发简单实例的更多相关文章

  1. Spring笔记——Spring+JDBC组合开发

      使用Spring+JDBC集成步骤如下:   1. 配置数据源 2. 配置事务.配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式 ...

  2. Springmvc+Spring+Mybatis整合开发(架构搭建)

    Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...

  3. SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。

    SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...

  4. (转)Spring+JDBC组合开发

    http://blog.csdn.net/yerenyuan_pku/article/details/52882435 搭建和配置Spring与JDBC整合的环境 使用Spring+JDBC集成步骤如 ...

  5. Java Web开发中Spring+MyBatis框架的简单搭建

    这里使用的eclipse,首先创建一个动态web项目. 1.导入Spring IOC.AOP.DAO.dbcp.dbdrive.mybatis.jar . mybatis-spring.jar  本人 ...

  6. 在Spring+MyBatis组合中使用事务

     通过Spring和MyBatis的组合,给出一个较为详细的实例 代码清单:配置Spring+MyBatis测试环境 <?xml version='1.0' encoding='UTF-8' ? ...

  7. Spring学习笔记--spring+mybatis集成

    前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...

  8. 最新 Eclipse IDE下的Spring框架配置及简单实例

    前段时间开始着手学习Spring框架,又是买书又是看视频找教程的,可是鲜有介绍如何配置Spring+Eclipse的方法,现在将我的成功经验分享给大家. 本文的一些源代码来源于码农教程:http:// ...

  9. Spring+Mybatis+Maven+MySql搭建实例

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+MySql的的搭建实例 ...

随机推荐

  1. selenium 实现浏览器 前进和后退

    #coding=utf-8from selenium import webdriver driver = webdriver.Chrome()driver.get("https://www. ...

  2. AutoFac文档7(转载)

    目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 适配器 和 装饰器 A ...

  3. C++加载dll失败或显示乱码

    右键项目-属性-字符集-使用多字节字符集

  4. 每日英语:How to find the career of your dreams

    The fate described by Dostoyevsky is a nightmare we all hope to escape. But we're surrounded by nays ...

  5. python判断一个对象是否可迭代

    如何判断一个对象是可迭代对象? 方法是通过collections模块的Iterable类型判断: >>> from collections import Iterable >& ...

  6. Eclipse “cannot be resolved to a type” error

    引言:     eclipse新导入的项目经常可以看到"XX cannot be resolved to a type"的报错信息.本文将做以简单总结. 正文:     (1)jd ...

  7. C++-教程1-VS2010环境设置

    一.需要下载的软件 1.visual studio 2010\\xxzx\tools\编程工具\MICROSOFT\VISUAL.STUDIO\VISUAL.STUDIO.201032位cn_visu ...

  8. love2d 0.9发布

    2013年12月13(有点遗憾,一个星期后才知道),love2d终于发布新版本了, 可以直接从我的百度网盘下载. 主要的更新有:(简单翻译自官方论坛说明) LuaJIT: 默认使用LuaJIT,性能大 ...

  9. lua自用的函数收集

    这里记录一下我常用到的一些lua函数,不定期更新. 1.cirleAdd函数是用来一个循环自增的,其中num是最大值, startNum是起始值,stepNum是步长,startFlag默认真起始值从 ...

  10. LeetCode -- 推断链表中是否有环

    思路: 使用两个节点.slow和fast,分别行进1步和2步.假设有相交的情况,slow和fast必定相遇:假设没有相交的情况,那么slow或fast必定有一个为null 相遇时有两种可能:1. 仅仅 ...