package com.zxh.customer.hashmap.dao;
import java.util.List;
import java.util.Map;
/**
*
* @author Administrator
*
* @param <T>
*/
public interface IBaseDao {
/**
* 单表插入记录
* @param obj
*/
public <T> int insert(String _mybitsId, T obj);
/**
* 更新单表
* @param obj
*/
public <T> int update(String _mybitsId, T obj);
/**
* 删除记录
* @param clz
* @param id
*/
public <T> int delete(String _mybitsId, T obj);
/**
*
* 返回查询一览表的信息
* @param <T>
* @param _mybitsId mybatis中对应业务标识
* @param _params
* @return
*/
public <T> List<T> query(String _mybitsId, Map<String, Object> _params);
/**
* 查询相关列表信息
* @param <T> 返回数据
* @param id mybatis中对应业务标识
* @param _params
* @return
*/
public <T> List<T> query(String _mybitsId, Object _params);
/**
* 查询单个数据
* @param queryString
* @param object
* @return
*/
public Object queryOne(String queryString, Object object);
}
package com.zxh.customer.hashmap.dao;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;
import com.googlecode.ehcache.annotations.Cacheable;
import com.googlecode.ehcache.annotations.TriggersRemove;
@Repository("baseDao")
public class BaseDaoImpl extends SqlSessionDaoSupport implements IBaseDao {
/**
* 获取相关的数据库连接
*/
public Connection getConnection() {
return getSqlSession().getConnection();
}
@TriggersRemove(cacheName = "springEhcache", removeAll = true)
public <T> int delete(String _mybitsId, T obj) {
return getSqlSession().delete(_mybitsId, obj);
}
@TriggersRemove(cacheName = "springEhcache", removeAll = true)
public <T> int insert(String _mybitsId, T obj) {
return getSqlSession().insert(_mybitsId, obj);
}
@TriggersRemove(cacheName = "springEhcache", removeAll = true)
public <T> int update(String _mybitsId, T obj) {
return getSqlSession().update(_mybitsId, obj);
}
@Cacheable(cacheName = "springEhcache")
public <T> List<T> query(String _mybitsId, Map<String, Object> _params) {
return getSqlSession().selectList(_mybitsId, _params);
}
@Cacheable(cacheName = "springEhcache")
public <T> List<T> query(String _mybitsId, Object _params) {
return getSqlSession().selectList(_mybitsId, _params);
}
@Cacheable(cacheName = "springEhcache")
public Object queryOne(String _mybitsId, Object object) {
return getSqlSession().selectOne(_mybitsId, object);
}
}
<?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="com.zxh.customer.hashmap.model.user">
<select id="selectUserById" parameterType="hashmap" resultType="hashmap">
SELECT CUST_ID, CUST_NAME FROM CUST WHERE
CUST_ID = #{custId}
</select>
<select id="selectUsersByName" parameterType="hashmap"
resultType="hashmap">
SELECT CUST_ID, CUST_NAME FROM CUST WHERE
CUST_NAME =
#{custName}
</select>
<insert id="insertUser" parameterType="hashmap">
insert into cust(cust_id,
cust_name) values(seq_cust.nextval, #{custName})
</insert>
<delete id="deleteUser" parameterType="hashmap">
delete from cust where
cust_id = #{custId}
</delete>
<update id="updateUser" parameterType="hashmap">
update cust set cust_name
= #{custName} where cust_id = #{custId}
</update>
</mapper>
package com.zxh.customer.hashmap.service;
public interface IBaseService {
}
package com.zxh.customer.hashmap.service;
import javax.annotation.Resource;
import com.zxh.customer.hashmap.dao.IBaseDao;
public class BaseServiceImpl implements IBaseService {
@Resource
protected IBaseDao baseDao;
public IBaseDao getBaseDao() {
return baseDao;
}
public void setBaseDao(IBaseDao baseDao) {
this.baseDao = baseDao;
}
}
package com.zxh.customer.hashmap.service;
import java.util.List;
import java.util.Map;
public interface IUserService extends IBaseService {
/**
* 获取单条记录
*/
public Map queryUserById(Map map);
/**
* 获取多条记录
*/
public List<Map> queryUsers(Map map);
/**
* 插入记录
* @throws Exception
*/
public void insertUser(Map map) throws Exception;
/**
* 删除记录
* @throws Exception
*/
public void deleteUser(Map map) throws Exception;
/**
* 更新记录
*/
public void updateUser(Map map);
}
package com.zxh.customer.hashmap.service;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl implements IUserService {
/**
* Map map 也可以定义领域模型实体类
*/
@Override
public Map queryUserById(Map map) {
return (Map) this.baseDao.queryOne(
"com.zxh.customer.hashmap.model.user.selectUserById", map);
}
@Override
public List<Map> queryUsers(Map map) {
return this.baseDao.query(
"com.zxh.customer.hashmap.model.user.selectUsersByName", map);
}
/**
* 如果没有配置@Transactional 使用sqlSession的jdbc事务 每一个操作都是一个单独的事务
* 抛出异常事务也不会回滚 无论抛出什么异常了
*/
@Transactional
public void insertUser(Map map) throws Exception {
this.baseDao.insert("com.zxh.customer.hashmap.model.user.insertUser",
map);
// 只有抛出运行期异常 事务才会回滚, 如果抛出Exception不会回滚了
// throw new RuntimeException();
}
@Transactional
public void deleteUser(Map map) throws Exception {
this.baseDao.delete("com.zxh.customer.hashmap.model.user.deleteUser",
map);
throw new Exception();
}
@Override
public void updateUser(Map map) {
this.baseDao.update("com.zxh.customer.hashmap.model.user.updateUser",
map);
}
}
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.zxh.customer.hashmap" />
<context:property-placeholder
location="classpath:sysconfig/jdbc_config.properties" />
<!-- 使用ehcache缓存 -->
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:sysconfig/ehcache.xml" />
</bean>
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<!--JNDI数据源配置 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"> <value>jdbc/JTORDER</value> </property> </bean> -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="jtorder" />
<property name="password" value="jtorder" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations"
value="classpath*:com/zxh/customer/hashmap/model/**/*.xml" />
<property name="configLocation" value="classpath:sysconfig/Configuration.xml" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
<!-- 使用SqlSessionDaoSupport 需要注入sqlSessionFactory -->
<bean id="baseDao" class="com.zxh.customer.hashmap.dao.BaseDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
<?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>
<!-- 设置mybatis3 运行时的行为方式 -->
<settings>
<!-- 设置超时时间,它决定驱动等待一个数据库响应的时间 -->
<setting name="defaultStatementTimeout" value="60000" />
<!-- 启用或禁用 缓存 -->
<setting name="cacheEnabled" value="false" />
<!-- 启用或禁用延迟加载。当禁用时, 所有关联对象都会即时加载 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 等等 -->
</settings>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- name:Cache的唯一标识 maxElementsInMemory:内存中最大缓存对象数 maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大
eternal:Element是否永久有效,一但设置了,timeout将不起作用 overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中
timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大
timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大
diskPersistent:是否缓存虚拟机重启期数据 diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)
备注: 持久化到硬盘的路径由虚拟机参数"java.io.tmpdir"决定. 例如, 在windows中, 会在此路径下 C:\Documents
and Settings\li\Local Settings\Temp 在linux中, 通常会在: /tmp 下 System.out.println(System.getProperty("java.io.tmpdir")); -->
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cache name="springEhcache" maxElementsInMemory="1000" eternal="false"
overflowToDisk="false" timeToIdleSeconds="600" timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@172.16.1.241\:1521\:jtcrm
username=jtorder
password=jtorder
package springtest;
import static org.junit.Assert.fail;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.zxh.customer.hashmap.service.IUserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:sysconfig/applicationContext.xml")
public class UserServiceImplTest {
@Resource
private IUserService userService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testQueryUserById() {
Map map = new HashMap();
map.put("custId", 604763L);
Map resultMap = this.userService.queryUserById(map);
System.out.println(resultMap.get("CUST_NAME"));
}
@Test
public void testInsertUser() throws Exception {
Map map = new HashMap();
map.put("custName", "大方地");
this.userService.insertUser(map);
}
@Test
public void testQueryUserById1() {
Map map = new HashMap();
map.put("custId", 604763L);
Map resultMap = this.userService.queryUserById(map);
System.out.println(resultMap.get("CUST_NAME"));
}
@Test
@Ignore
public void testDeleteUser() {
fail("Not yet implemented");
}
@Test
@Ignore
public void testUpdateUser() {
fail("Not yet implemented");
}
}