spring+springMVC+mybatis基础代码

web.xml配置

 

Xml代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  5.      
  6.    <!-- ==========================spring lo4j相关配置 开始========================================= -->  
  7.    <!--log4j配置文件加载-->    
  8.    <context-param>        
  9.        <param-name>log4jConfigLocation</param-name>        
  10.        <param-value>classpath:log4j.properties</param-value>        
  11.    </context-param>    
  12.    <!--启动一个watchdog线程每1800秒扫描一下log4j配置文件的变化-->    
  13.    <context-param>        
  14.        <param-name>log4jRefreshInterval</param-name>        
  15.        <param-value>1800000</param-value>        
  16.    </context-param>     
  17.    <!--spring log4j监听器-->    
  18.    <listener>  
  19.        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>        
  20.    </listener>  
  21.    <!-- ==========================spring lo4j相关配置 结束========================================= -->  
  22.      
  23.    <!-- ==========================spring mvc context相关配置 开始========================================= -->  
  24.    <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->  
  25.     <context-param>  
  26.         <param-name>contextConfigLocation</param-name>  
  27.         <param-value>classpath:spring-main.xml</param-value>  
  28.     </context-param>    
  29.      
  30.     <!-- Spring MVC配置 -->  
  31.     <servlet>  
  32.         <description>可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,  
  33.         名称为如spring-servlet.xml</description>  
  34.         <servlet-name>spring mvc</servlet-name>  
  35.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  36.         <init-param>  
  37.             <param-name>contextConfigLocation</param-name>  
  38.             <param-value>classpath:spring-mvc.xml</param-value>  
  39.         </init-param>  
  40.          <load-on-startup>1</load-on-startup>  
  41.     </servlet>  
  42.       
  43.     <servlet-mapping>  
  44.         <servlet-name>spring mvc</servlet-name>  
  45.         <url-pattern>*.do</url-pattern>  
  46.     </servlet-mapping>  
  47.       
  48.     <!-- Spring配置 -->  
  49.     <listener>  
  50.        <listener-class>  
  51.          org.springframework.web.context.ContextLoaderListener  
  52.        </listener-class>  
  53.     </listener>  
  54.       
  55.    <!-- ==========================spring mvc context相关配置 结束========================================= -->  
  56.       
  57.     <welcome-file-list>  
  58.         <welcome-file>/index.html</welcome-file>  
  59.         <welcome-file>/index.jsp</welcome-file>  
  60.     </welcome-file-list>  
  61. </web-app>  

 spring-main.xml spring综合配置文件

 

 

Xml代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  7.       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd     
  8.       http://www.springframework.org/schema/context     
  9.       http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  10.       http://www.springframework.org/schema/tx  
  11.       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">  
  12.     <description>定义业务层和集成层对象,包括Action、Service、BO、DAO、SAO、EJB、JNDI资源  
  13.     </description>  
  14.     <!--===================================================================== -->  
  15.     <!-- 配置外部变量文件 -->  
  16.     <!--===================================================================== -->  
  17.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  18.         <property name="location">  
  19.             <description>可以引用CLASSPATH中common-spring.properties中定义的变量  
  20.             </description>  
  21.             <value>classpath:common-spring.properties</value>  
  22.         </property>  
  23.         <property name="ignoreUnresolvablePlaceholders" value="true" />  
  24.     </bean>  
  25.   
  26.     <!--修改为本项目的根包名,这点很重要 -->  
  27.     <context:component-scan base-package="com.fast.base"></context:component-scan>  
  28.     <context:component-scan base-package="com.fast.app"></context:component-scan>  
  29.   
  30.     <!-- 引入其它配置文件 -->  
  31.     <!-- 
  32.     <import resource="spring-mvc.xml"/> 
  33.     -->  
  34.     <import resource="spring-orm.xml"/>  
  35. </beans>  

 

 

 

spring-mvc.xml 配置

 

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans  
  8.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/mvc  
  10.         http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  11.         http://www.springframework.org/schema/context  
  12.         http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
  13.     <description>此配置对应spring mvc相关拦截配置</description>  
  14.       
  15.     <!--开启扫描包和子包 -->  
  16.     <context:component-scan base-package="com.fast.app.web"/>  
  17.    
  18.     <!-- 使用默认的Servlet来响应静态文件 -->  
  19.     <mvc:default-servlet-handler />  
  20.    
  21.     <!-- 注解支持 -->  
  22.     <mvc:annotation-driven />  
  23.        
  24.     <!-- configure the InternalResourceViewResolver -->  
  25.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">  
  26.         <!-- 前缀 -->  
  27.         <property name="prefix" value="/pages" />  
  28.         <!-- 后缀 -->  
  29.         <property name="suffix" value=".jsp" />  
  30.     </bean>  
  31.       
  32.     <!-- 拦截器设置 -->  
  33.     <mvc:interceptors>  
  34.         <mvc:interceptor>  
  35.             <mvc:mapping path="/*.do"/>  
  36.             <bean class="com.fast.app.web.interceptor.CommonInterceptor"></bean>  
  37.         </mvc:interceptor>          
  38.     </mvc:interceptors>  
  39.       
  40. </beans>  

 

 

spring-orm.xml  数据源相关,数据访问相关,事务配置

 

Xml代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  7.       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd     
  8.       http://www.springframework.org/schema/context     
  9.       http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  10.       http://www.springframework.org/schema/tx  
  11.       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">  
  12.     <description>此配置文件主要针对数据库连接,事务,session控制等等</description>  
  13.       
  14.     <!--===================================================================== -->  
  15.     <!-- 数据源定义 -->  
  16.     <!--===================================================================== -->  
  17.     <!-- TODO: 如果使用到数据源,请将dsFactory的定义打开,并正确配置数据源JNDI  -->  
  18.     <!-- C3P0连接池配置 oracle-->  
  19.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  20.         <property name="driverClass" value="${jdbc.driverClass}" />  
  21.         <property name="jdbcUrl" value="${jdbc.jdbcUrl}" />  
  22.         <property name="user" value="${jdbc.user}" />  
  23.         <property name="password" value="${jdbc.password}" />  
  24.         <!--连接池中保留的最小连接数。-->  
  25.         <property name="minPoolSize" value="${jdbc.miniPoolSize}" />  
  26.         <!--连接池中保留的最大连接数。-->  
  27.         <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>   
  28.         <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->  
  29.         <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>  
  30.         <!--最大空闲时间,设置值内未使用则连接被丢弃。若为0则永不丢弃。 -->    
  31.         <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>  
  32.         <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。-->   
  33.         <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>  
  34.           
  35.         <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->  
  36.         <property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}"/>  
  37.         <!--两次连接中间隔时间,单位毫秒。Default: 1000 -->  
  38.         <property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}"/>  
  39.         <!--如果设为true那么在取得连接的同时将校验连接的有效性。Default: false -->  
  40.         <property name="testConnectionOnCheckin" value="${jdbc.testConnectionOnCheckin}"/>  
  41.         <!--<property name="automaticTestTable" value="${jdbc.automaticTestTable}"/>-->  
  42.         <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->  
  43.         <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/>  
  44.         <!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出  
  45.             SQLException,如设为0则无限期等待。单位毫秒。Default: 0 -->  
  46.         <property name="checkoutTimeout" value="${jdbc.checkoutTimeout}"/>  
  47.         <!--定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意:测试的表必须在初始数据源的时候就存在。Default: null-->  
  48.         <property name="preferredTestQuery" value="${jdbc.preferredTestQuery}" />  
  49.     </bean>  
  50.     <!--===================================================================== -->  
  51.     <!-- MYBATIS 配置文件定义 -->  
  52.     <!--===================================================================== -->  
  53.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    
  54.         <property name="dataSource" ref="dataSource" />    
  55.         <property name="configLocation" value="classpath:mybatis-config.xml"></property>    
  56.     </bean>    
  57.       
  58.     <bean id="sqlSession"  class="org.mybatis.spring.SqlSessionTemplate">     
  59.        <constructor-arg index="0" ref="sqlSessionFactory" />     
  60.     </bean>    
  61.       
  62.     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->    
  63.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
  64.         <property name="basePackage" value="com.fast.base.dao" />  
  65.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>    
  66.     </bean>   
  67.       
  68.     <!-- 定义事务处理类,不同的数据访问方式,事务处理类不同    
  69.         比如:Hibernate操作的HibernateTransactionManager,JDBC操作的使用DataSourceTransactionManager    
  70.      -->    
  71.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
  72.         <property name="dataSource" ref="dataSource"></property>    
  73.     </bean>    
  74.     <!-- 声明使用注解式事务 -->    
  75.     <tx:annotation-driven transaction-manager="transactionManager"/>    
  76.       
  77. </beans>  

 

 

 

common-spring.properties 综合配置文件

 

Java代码  
  1. #C3P0 连接池配置  
  2. jdbc.driverClass=oracle.jdbc.driver.OracleDriver  
  3. jdbc.jdbcUrl=jdbc:oracle:thin:@10.20.128.115:1526:d0rsapp  
  4. jdbc.user=elisdata  
  5. jdbc.password=ilovetong  
  6. #连接池中保留的最小连接数。   
  7. jdbc.miniPoolSize= 20  
  8. #连接池中保留的最大连接数。   
  9. jdbc.maxPoolSize= 50  
  10. #初始化时获取N个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3    
  11. jdbc.initialPoolSize= 20  
  12. #最大空闲时间,设置值内未使用则连接被丢弃。若为0则永不丢弃。      
  13. jdbc.maxIdleTime= 25000  
  14. #当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。    
  15. jdbc.acquireIncrement= 3  
  16.   
  17. #定义在从数据库获取新连接失败后重复尝试的次数。Default: 30    
  18. jdbc.acquireRetryAttempts= 30  
  19. #两次连接中间隔时间,单位毫秒。Default: 1000   
  20. jdbc.acquireRetryDelay= 1000  
  21. #如果设为true那么在取得连接的同时将校验连接的有效性。Default: false    
  22. jdbc.testConnectionOnCheckin= true  
  23. #c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试使用。  
  24. jdbc.automaticTestTable= c3p0TestTable  
  25. #当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。Default: 0    
  26. jdbc.idleConnectionTestPeriod= 18000  
  27. #定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。  
  28. #注意:测试的表必须在初始数据源的时候就存在。Default: null,该配置项不可以和automaticTestTable同时使用  
  29. jdbc.preferredTestQuery=select id from test_c3p0 where id=1  
  30. #超时时间  
  31. jdbc.checkoutTimeout=3000  

 

 

 

log4j.properties 日志配置相关

 

Java代码  
  1. #ibatis log4j运行级别调到DEBUG可以在控制台打印出ibatis运行的sql语句,方便调试:  
  2. ### 设置Logger输出级别和输出目的地 ###  
  3. log4j.rootLogger=info,stdout,logfile  
  4. ### 把日志信息输出到控制台 ###  
  5. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
  6. #log4j.appender.stdout.Target=System.err  
  7. log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout  
  8.   
  9. ### 把日志信息输出到文件:default ###  
  10. log4j.appender.logfile=org.apache.log4j.FileAppender  
  11. log4j.appender.logfile.File=${catalina.home}/logs/default.log  
  12. log4j.appender.logfile.layout=org.apache.log4j.PatternLayout  
  13. log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n  
  14.   
  15. ###显示SQL语句部分  
  16. log4j.logger.com.mybatis=DEBUG  
  17. log4j.logger.com.mybatis.common.jdbc.SimpleDataSource=DEBUG  
  18. log4j.logger.com.mybatis.common.jdbc.ScriptRunner=DEBUG  
  19. log4j.logger.com.mybatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG  
  20. log4j.logger.java.sql.Connection=DEBUG  
  21. log4j.logger.java.sql.Statement=DEBUG  
  22. log4j.logger.java.sql.PreparedStatement=DEBUG  
  23.   
  24. #job  
  25. log4j.logger.com.jinguanjia.task.service.impl=INFO,task  
  26. log4j.appender.task=org.apache.log4j.DailyRollingFileAppender  
  27. log4j.appender.task.File=${catalina.home}/logs/task.log  
  28. log4j.appender.task.Append=true  
  29. log4j.appender.task.layout=org.apache.log4j.PatternLayout   
  30. log4j.appender.task.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}|%t|%-5p|%c,%L,%M|->%m%n  

 

 

mybatis-config.xml mybatis综合配置文件

 

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE configuration PUBLIC    
  3.     "-//mybatis.org//DTD Config 3.0//EN"    
  4.     "http://mybatis.org/dtd/mybatis-3-config.dtd">    
  5. <configuration>  
  6.   <typeAliases>  
  7.       <!--  
  8.       通过package, 可以直接指定package的名字, mybatis会自动扫描你指定包下面的javabean,  
  9.       并且默认设置一个别名,默认的名字为: javabean 的首字母小写的非限定类名来作为它的别名。  
  10.       也可在javabean 加上注解@Alias 来自定义别名, 例如: @Alias(user)   
  11.       <package name="com.dy.entity"/>  
  12.        -->  
  13.       <typeAlias alias="PRODUCTINFO" type="com.fast.app.model.ProductInfo" />  
  14.   </typeAliases>  
  15.         
  16.     <mappers>    
  17.         <mapper resource="com/fast/app/mapper/ProductInfoDaoMapper.xml"/>    
  18.     </mappers>    
  19. </configuration>  

 

 

基础Dao

 

Java代码  
  1. package com.fast.base.dao;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. /** 
  7.  * mybatis基础Dao 
  8.  * @author LUSHUIFA242 
  9.  * 
  10.  */  
  11. public interface BaseDao{  
  12.   
  13.     /** 
  14.      * 对对象进行持久化操作,如果成功则返回1 
  15.      * 失败返回-1 
  16.      * @param obj 
  17.      * @return 
  18.      */  
  19.     <T> int save(T bean);  
  20.       
  21.     /** 
  22.      * 批量保存 
  23.      * @param objList 
  24.      * @return 成功返回操作的成功的数量 失败则回滚并返回-1 
  25.      */  
  26.     <T> int save(List<T> beanList);  
  27.       
  28.     /** 
  29.      * 对对象进行持久化操作,如果成功则返回1 
  30.      * 失败返回-1 
  31.      * @param obj 
  32.      * @return 
  33.      */  
  34.     <T> int saveOrUpdate(T bean);  
  35.       
  36.     /** 
  37.      * 批量保存与更新  
  38.      * @param objList 
  39.      * @return 成功返回操作的成功的数量 失败则回滚并返回-1 
  40.      */  
  41.     <T> int saveOrUpdate(List<T> beanList);  
  42.       
  43.      /** 
  44.      * 修改指定的持久化对象 
  45.      * @param id 
  46.      * @param obj 
  47.      */  
  48.     <T> int update(T bean);  
  49.       
  50.     /** 
  51.      * 修改指定的持久化对象,批量 
  52.      * @param objList 
  53.      * @return 
  54.      */  
  55.     <T> int update(List<T> beanList);  
  56.       
  57.     /** 
  58.      * 删除指定id的持久化对象 
  59.      * @param id 
  60.      * @return  
  61.      */  
  62.     <T> int delete(Object primaryKey);  
  63.      
  64.       
  65.     /** 
  66.      * 返回持久化对象 
  67.      * @param obj 
  68.      * @return 找到则返回,否则返回空 
  69.      */  
  70.     <T> T queryObject(T bean);  
  71.       
  72.     /** 
  73.      * 返回持久化对象 
  74.      * @param primaryKey 
  75.      * @return 找到则返回,否则返回空 
  76.      */  
  77.     <T> T queryObjectByPrimaryKey(Object primaryKey);  
  78.       
  79.     /** 
  80.      * 返回持久化对象 
  81.      * @param queryParam 
  82.      * @return 找到则返回,否则返回空 
  83.      */  
  84.     <T> T queryObjectByParam(Map<String,Object> queryParam);  
  85.       
  86.     /** 
  87.      * 返回列表  
  88.      * @param queryParam 
  89.      * @return 找到则返回,否则返回空 
  90.      */  
  91.     <T> List<T> queryList(T bean);  
  92.       
  93.     /** 
  94.      * 返回列表  
  95.      * @param bean 
  96.      * @param limitSize 
  97.      * @return 找到则返回,否则返回空 
  98.      */  
  99.     <T> List<T> queryList(T bean,int limitSize);  
  100.       
  101.     /** 
  102.      * 返回列表  
  103.      * @param bean 
  104.      * @param startIndex 
  105.      * @param endIndex 
  106.      * @return 找到则返回,否则返回空 
  107.      */  
  108.     <T> List<T> queryList(T bean,int startIndex,int endIndex);  
  109.       
  110.     /** 
  111.      * 返回列表  
  112.      * @param queryParam 
  113.      * @return 找到则返回,否则返回空 
  114.      */  
  115.     <T> List<T> queryListByParam(Map<String,Object> queryParam);  
  116.       
  117.     /** 
  118.      * 返回列表  
  119.      * @param queryParam 
  120.      * @param limitSize 
  121.      * @return 找到则返回,否则返回空 
  122.      */  
  123.     <T> List<T> queryListByParam(Map<String,Object> queryParam,int limitSize);  
  124.       
  125.     /** 
  126.      * 返回列表  
  127.      * @param queryParam 
  128.      * @param startIndex 
  129.      * @param endIndex 
  130.      * @return 找到则返回,否则返回空 
  131.      */  
  132.     <T> List<T> queryListByParam(Map<String,Object> queryParam,int startIndex,int endIndex);  
  133.       
  134.     /** 
  135.      * 统计所有记录数 
  136.      * @return 
  137.      */  
  138.     <T> long count();  
  139.       
  140.     /** 
  141.      * 根据条件统计 
  142.      * @param bean 
  143.      * @return 
  144.      */  
  145.     <T> long count(T bean);  
  146.       
  147.     void setXmlName(String xmlName);  
  148.       
  149. }  

 

 

基础dao实现类

 

Java代码  
  1. package com.fast.base.dao.impl;  
  2.   
  3. import java.sql.Connection;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import javax.annotation.Resource;  
  9.   
  10. import org.apache.ibatis.session.SqlSession;  
  11. import org.springframework.stereotype.Component;  
  12.   
  13. import com.alibaba.fastjson.JSONObject;  
  14. import com.fast.base.dao.BaseDao;  
  15.   
  16. @Component  
  17. public class BaseDaoImpl implements BaseDao {  
  18.   
  19.     @Resource(name = "sqlSession")  
  20.     private SqlSession session;  
  21.     private String path = "com.fast.app.mapper.";  
  22.       
  23.     private String xmlName;  
  24.       
  25.     protected BaseDaoImpl(){  
  26.         path = path + this.getXmlName();  
  27.     }  
  28.       
  29.     protected BaseDaoImpl(String xmlName) {  
  30.         this.setXmlName(xmlName);  
  31.         path = path + xmlName;  
  32.     }  
  33.       
  34.     protected String getMethodPath(String methodName) {  
  35.         return path +"."+ methodName;  
  36.     }  
  37.       
  38.     /** 
  39.      * 对对象进行持久化操作,如果成功则返回1 
  40.      * 失败返回-1 
  41.      * @param obj 
  42.      * @return 
  43.      */  
  44.     public <T> int save(T bean){  
  45.         return this.session.insert(getMethodPath("save"), bean);  
  46.     }  
  47.       
  48.     /** 
  49.      * 批量保存 
  50.      * @param objList 
  51.      * @return 成功返回操作的成功的数量 失败则回滚并返回-1 
  52.      */  
  53.     public <T> int save(List<T> beanList) {  
  54.         return this.session.insert(getMethodPath("batchSave"), beanList);  
  55.     }  
  56.       
  57.     /** 
  58.      * 对对象进行持久化操作,如果成功则返回1 
  59.      * 失败返回-1 
  60.      * @param obj 
  61.      * @return 
  62.      */  
  63.     public <T> int saveOrUpdate(T bean) {  
  64.         return this.session.insert(getMethodPath("saveOrUpdate"), bean);  
  65.     }  
  66.       
  67.     /** 
  68.      * 批量保存与更新  
  69.      * @param objList 
  70.      * @return 成功返回操作的成功的数量 失败则回滚并返回-1 
  71.      */  
  72.     public <T> int saveOrUpdate(List<T> beanList) {  
  73.         int actionCount = -1;  
  74.         for (T t : beanList) {  
  75.             if(saveOrUpdate(t)>0){  
  76.                 actionCount++;  
  77.             }  
  78.         }  
  79.         return actionCount;  
  80.     }  
  81.       
  82.      /** 
  83.      * 修改指定的持久化对象 
  84.      * @param id 
  85.      * @param obj 
  86.      */  
  87.     public <T> int update(T bean) {  
  88.         return this.session.update(getMethodPath("update"), bean);  
  89.     }  
  90.       
  91.     /** 
  92.      * 修改指定的持久化对象,批量 
  93.      * @param objList 
  94.      * @return 
  95.      */  
  96.     public <T> int update(List<T> beanList) {  
  97.         return this.session.update(getMethodPath("batchUpdate"), beanList);  
  98.     }  
  99.       
  100.     /** 
  101.      * 删除指定id的持久化对象 
  102.      * @param id 
  103.      */  
  104.     public <T> int delete(Object primaryKey) {  
  105.         return this.session.delete(getMethodPath("deleteByPrimaryKey"), primaryKey);  
  106.           
  107.     }  
  108.      
  109.     /** 
  110.      * 返回持久化对象 
  111.      * @param obj 
  112.      * @return 找到则返回,否则返回空 
  113.      */  
  114.     public <T> T queryObject(T bean) {  
  115.         return this.session.selectOne(getMethodPath("queryObject"), bean);  
  116.     }  
  117.       
  118.     /** 
  119.      * 返回持久化对象 
  120.      * @param primaryKey 
  121.      * @return 找到则返回,否则返回空 
  122.      */  
  123.     public <T> T queryObjectByPrimaryKey(Object primaryKey) {  
  124.         return this.session.selectOne(getMethodPath("queryObjectByPrimaryKey"), primaryKey);  
  125.     }  
  126.       
  127.     /** 
  128.      * 返回持久化对象 
  129.      * @param queryParam 
  130.      * @return 找到则返回,否则返回空 
  131.      */  
  132.     public <T> T queryObjectByParam(Map<String,Object> queryParam) {  
  133.         return this.session.selectOne(getMethodPath("queryObjectByParam"), queryParam);  
  134.     }  
  135.       
  136.     /** 
  137.      * 返回列表  
  138.      * @param queryParam 
  139.      * @return 找到则返回,否则返回空 
  140.      */  
  141.     public <T> List<T> queryList(T bean) {  
  142.         return queryList(bean,-1);  
  143.     }  
  144.       
  145.     /** 
  146.      * 返回列表  
  147.      * @param bean 
  148.      * @param limitSize -1 
  149.      * @return 找到则返回,否则返回空 
  150.      */  
  151.     public <T> List<T> queryList(T bean,int limitSize) {  
  152.         try {  
  153.             Map<String,Object> param = new HashMap<String,Object>();  
  154.             param.putAll((Map<? extends String, ? extends Object>) JSONObject.toJSON(bean));  
  155.             param.put("limitSize", limitSize);  
  156.             return this.session.selectList(getMethodPath("queryList"), param);  
  157.         } catch (Exception e) {  
  158.             e.printStackTrace();  
  159.             return null;  
  160.         }  
  161.     }  
  162.       
  163.     /** 
  164.      * 返回列表 此方法适用分页 
  165.      * @param bean 
  166.      * @param startIndex 
  167.      * @param endIndex 
  168.      * @return 找到则返回,否则返回空 
  169.      */  
  170.     public <T> List<T> queryList(T bean,int startIndex,int endIndex){  
  171.         Map<String,Object> param = new HashMap<String,Object>();  
  172.         param.putAll((Map<? extends String, ? extends Object>) JSONObject.toJSON(bean));  
  173.         param.put("startIndex", startIndex);  
  174.         param.put("endIndex", endIndex);  
  175.         return this.session.selectList(getMethodPath("queryListInRange"), param);  
  176.     }  
  177.       
  178.     /** 
  179.      * 返回列表  
  180.      * @param queryParam 
  181.      * @return 找到则返回,否则返回空 
  182.      */  
  183.     public <T> List<T> queryListByParam(Map<String,Object> queryParam){  
  184.         return this.session.selectList(getMethodPath("queryListByParam"), queryParam);  
  185.     }  
  186.       
  187.     /** 
  188.      * 返回列表  
  189.      * @param queryParam 
  190.      * @param limitSize 
  191.      * @return 找到则返回,否则返回空 
  192.      */  
  193.     public <T> List<T> queryListByParam(Map<String,Object> queryParam,int limitSize){  
  194.         queryParam.put("limitSize", limitSize);  
  195.         return this.session.selectList(getMethodPath("queryListByParam"), queryParam);  
  196.     }  
  197.       
  198.     /** 
  199.      * 返回列表  
  200.      * @param queryParam 
  201.      * @param startIndex 
  202.      * @param endIndex 
  203.      * @return 找到则返回,否则返回空 
  204.      */  
  205.     public <T> List<T> queryListByParam(Map<String,Object> queryParam,int startIndex,int endIndex){  
  206.         queryParam.put("startIndex", startIndex);  
  207.         queryParam.put("endIndex", endIndex);  
  208.         return this.session.selectList(getMethodPath("queryListByParam"), queryParam);  
  209.     }  
  210.       
  211.     /** 
  212.      * 统计所有记录数 
  213.      * @return 
  214.      */  
  215.     public <T> long count(){  
  216.         return this.session.selectOne(getMethodPath("count"));  
  217.     }  
  218.       
  219.     /** 
  220.      * 根据条件统计 
  221.      * @param bean 
  222.      * @return 
  223.      */  
  224.     public <T> long count(T bean){  
  225.         return this.session.selectOne(getMethodPath("count"),bean);  
  226.     }  
  227.   
  228.     /** 
  229.      * 得到数据库连接 
  230.      * @return 
  231.      */  
  232.     public Connection getConn(){  
  233.         return this.getSession().getConnection();  
  234.     }  
  235.   
  236.     /** 
  237.      * 得到mybatis session操作类 
  238.      * @return 
  239.      */  
  240.     public SqlSession getSession() {  
  241.         return session;  
  242.     }  
  243.   
  244.     public void setSession(SqlSession session) {  
  245.         this.session = session;  
  246.     }  
  247.   
  248.     public String getXmlName() {  
  249.         return xmlName;  
  250.     }  
  251.   
  252.     @Override  
  253.     public void setXmlName(String xmlName) {  
  254.         this.xmlName = xmlName;  
  255.     }  
  256. }  

学习Java的同学注意了!!!
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:184625948  我们一起学Java!

转载于:https://my.oschina.net/abcijkxyz/blog/850931

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值