SpringMVC笔记——Spring+MyBatis组合开发简单实例
简介
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组合开发简单实例的更多相关文章
- Spring笔记——Spring+JDBC组合开发
使用Spring+JDBC集成步骤如下: 1. 配置数据源 2. 配置事务.配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式 ...
- Springmvc+Spring+Mybatis整合开发(架构搭建)
Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...
- SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。
SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...
- (转)Spring+JDBC组合开发
http://blog.csdn.net/yerenyuan_pku/article/details/52882435 搭建和配置Spring与JDBC整合的环境 使用Spring+JDBC集成步骤如 ...
- Java Web开发中Spring+MyBatis框架的简单搭建
这里使用的eclipse,首先创建一个动态web项目. 1.导入Spring IOC.AOP.DAO.dbcp.dbdrive.mybatis.jar . mybatis-spring.jar 本人 ...
- 在Spring+MyBatis组合中使用事务
通过Spring和MyBatis的组合,给出一个较为详细的实例 代码清单:配置Spring+MyBatis测试环境 <?xml version='1.0' encoding='UTF-8' ? ...
- Spring学习笔记--spring+mybatis集成
前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...
- 最新 Eclipse IDE下的Spring框架配置及简单实例
前段时间开始着手学习Spring框架,又是买书又是看视频找教程的,可是鲜有介绍如何配置Spring+Eclipse的方法,现在将我的成功经验分享给大家. 本文的一些源代码来源于码农教程:http:// ...
- Spring+Mybatis+Maven+MySql搭建实例
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+MySql的的搭建实例 ...
随机推荐
- nodejs 发起http请求
http://nodejs.cn/api/http.html#http_http_request_options_callback http://yijiebuyi.com/blog/8221eb14 ...
- Atitit.mssql 数据库表记录数and 表体积大小统计
Atitit.mssql 数据库表记录数and 表体积大小统计 1. EXEC sp_MSforeachtable "EXECUTE sp_spaceused '?'&quo ...
- Atitit.Gui控件and面板----db数据库区----- .数据库比较同步工具 vOa
Atitit.Gui控件and面板----db数据库区----- .数据库比较同步工具 vOa 1. 咨微海信数据库应用 工具 1 2. 数据库比较工具 StarInix SQL Compare ...
- android线程控制UI更新(Handler 、post()、postDelayed()、postAtTime)
依照以下的理解就是handler与ui线程有一定的关联能够由于更新界面仅仅能在主线程中全部更新界面的地方能够在接受消息的handleMessage那里还有更新界面能够在handler.port(new ...
- /^(0|[1-9]\d*)([.]5)?$/ 在PHP正则中是什么意思 ?
^以什么开头 ()分组 |或的意思 \d 匹配任何数字字符串 [-] |[-]\d* 或1-9之间的数+任意数字零次或多次 开头 ()分组 []原子表 [.]5匹配. ? 零次或1次 总结: 必须以0 ...
- Mac里面如何设置自启动服务
Mac OS x 启动项设置 Mac OS X的启动原理: 1,mac固件激活,初始化硬件,加载BootX引导器. 2,BootX加载内核与内核扩展(kext). 3,内核启动launchd进程. 4 ...
- MySQL编码问题集合
1.以root用户的身份登录,查看编码设置 mysql> SHOW VARIABLES LIKE 'character%'; +--------------------------+------ ...
- Oracle 错误:ORA-06413: Connection not open 解决办法
http://blog.csdn.net/neso520/article/details/6037411 ——————————————————————————————————————————————— ...
- 001杰信-创建MyEclipse与maven项目
准备工作: 自己的私人仓库:
- mysql -- 存储过程中 declare 和 set 定义变量的区别
mysql存储过程中,定义变量有两种方式:1.使用set或select直接赋值,变量名以 @ 开头.例如:set @var=1;可以在一个会话的任何地方声明,作用域是整个会话,称为会话变量. 2.以 ...