关闭

mybatis+spring3实战2-sqlSessionDaoSupport方式

7653人阅读 评论(2) 收藏 举报
分类:

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.util.List;
import java.util.Map;
import java.sql.Connection;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;


@Repository("baseDao")
public class BaseDaoImpl extends SqlSessionDaoSupport implements IBaseDao {
 
 /**
  * 获取相关的数据库连接
  */
 public Connection getConnection() {
  return getSqlSession().getConnection();
 }
 
 public <T> int delete(String _mybitsId, T obj) {
  return getSqlSession().delete(_mybitsId, obj);
 }

 @Override
 public <T> int insert(String _mybitsId, T obj) {
  return getSqlSession().insert(_mybitsId, obj);
 }
 @Override
 public <T> int update(String _mybitsId, T obj) {
  return getSqlSession().update(_mybitsId, obj);
 }

 @Override
 public <T> List<T> query(String _mybitsId, Map<String, Object> _params) {
  return getSqlSession().selectList(_mybitsId, _params);
 }
 @Override
 public <T> List<T> query(String _mybitsId, Object _params) {
  return getSqlSession().selectList(_mybitsId, _params);
 }
 
 @Override
 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"
 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">

 <context:annotation-config />
 <context:component-scan base-package="com.zxh.customer.hashmap" />
 <context:property-placeholder
  location="classpath:sysconfig/jdbc_config.properties" />

 <!--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>

 

driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@172.16.1.241\:1521\:jtcrm
username=jtorder
password=jtorder

 

 

 

 

 

package springtest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zxh.customer.hashmap.service.IUserService;

public class Test {

 public static void main(String[] args) throws Exception {

  ApplicationContext context = new ClassPathXmlApplicationContext(
    "sysconfig/applicationContext.xml");
  IUserService userService = (IUserService) context
    .getBean("userService");

  // Map map = new HashMap();
  // map.put("custId", "604763");
  // Map resultMap = userService.queryUserById(map);
  // System.out.println(resultMap.get("CUST_NAME"));

  // Map map = new HashMap();
  // map.put("custName", "测试测试");
  // List<Map> list = userService.queryUsers(map);
  // System.out.println(list.size());

  Map map = new HashMap();
  map.put("custName", "风纪扣");
  userService.insertUser(map);

  // Map map = new HashMap();
  // map.put("custId", "612035");
  // try {
  // userService.deleteUser(map);
  // } catch (Exception e) {
  // e.printStackTrace();
  // }

  System.out.println("成功");
 }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:388120次
    • 积分:3252
    • 等级:
    • 排名:第10829名
    • 原创:15篇
    • 转载:119篇
    • 译文:0篇
    • 评论:32条
    最新评论