一、配置jdcp.properties數據庫鏈接文件 #mysql database setting jdbc.type=mysql jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://1xxx:3306/xx?useUnicode=true&characterEncoding=utf-8 jdbc.username=xxx jdbc.password=xxx #pool settings jdbc.pool.init=1 jdbc.pool.minIdle=3 jdbc.pool.maxActive=20
1.1配置mybatis全局文件mybatis-config.xmlhtml
<?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"/> <!-- 全局啓用或禁用延遲加載。當禁用時,全部關聯對象都會即時加載。 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 當啓用時,有延遲加載屬性的對象在被調用時將會徹底加載任意屬性。不然,每種屬性將會按須要加載。 --> <setting name="aggressiveLazyLoading" value="true"/> <!-- 是否容許單條sql 返回多個數據集 (取決於驅動的兼容性) default:true --> <setting name="multipleResultSetsEnabled" value="true"/> <!-- 是否可使用列的別名 (取決於驅動的兼容性) default:true --> <setting name="useColumnLabel" value="true"/> <!-- 容許JDBC 生成主鍵。須要驅動器支持。若是設爲了true,這個設置將強制使用被生成的主鍵,有一些驅動器不兼容不過仍然能夠執行。 default:false --> <setting name="useGeneratedKeys" value="false"/> <!-- 指定 MyBatis 如何自動映射 數據基表的列 NONE:不隱射 PARTIAL:部分 FULL:所有 --> <setting name="autoMappingBehavior" value="PARTIAL"/> <!-- 這是默認的執行類型 (SIMPLE: 簡單; REUSE: 執行器可能重複使用prepared statements語句;BATCH: 執行器能夠重複執行語句和批量更新) --> <setting name="defaultExecutorType" value="SIMPLE"/> <!-- 使用駝峯命名法轉換字段。 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 設置本地緩存範圍 session:就會有數據的共享 statement:語句範圍 (這樣就不會有數據的共享 ) defalut:session --> <setting name="localCacheScope" value="SESSION"/> <!-- 設置但JDBC類型爲空時,某些驅動程序 要指定值,default:OTHER,插入空值時不須要指定類型 --> <setting name="jdbcTypeForNull" value="NULL"/> </settings> <!-- 類型別名 --> <typeAliases> <typeAlias alias="Page" type="com.yueqiu8.common.persistence.Page" /><!--分頁 --> </typeAliases> <!-- 插件配置 --> <plugins> <!--<頁面攔截器插件/>--> <plugin interceptor="com.github.pagehelper.PageHelper" /> </plugins> </configuration>
二、spring-context.xml文件配置(spring-context.xml配置在web.xml中加載) <!-- 加載classpath下的jdbc.properties文件,裏面配了數據庫鏈接的一些信息 --> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="${dataSource}" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!--數據源配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref bean="dataSource"/> <property name="typeAliasesPackage" value="com.xxx.path"/> <property name="typeAliasesSuperType" value="xxx.xxx"/> <property name="mapperLocations" value="classpath:/mappings/**/*.xml"/> <property name="configLocation" value="classpath:/mybatis-config.xml"></property> </bean> 配置sql,UserMapper.xml供service使用 <mapper namespace="ssm.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="user"> select * from user where id = #{id} </select> </mapper> 三、dao方式和mapper方式使用mybatis 在context-spring.xml配置不一樣的加載方式 3.一、dao方式: com.xxx.MybatisDao類以下: public class MybatisDao extends SqlSessionDaoSupport { final static Logger logger = LoggerFactory.getLogger(GenericDao.class); private SqlSessionFactory sqlSessionFactory; public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { super.setSqlSessionFactory(sqlSessionFactory); this.sqlSessionFactory = sqlSessionFactory; } public <T>T select(String key, Object param) { if(null == param) { return this.getSqlSession().selectOne(key); } return this.getSqlSession().selectOne(key,param); } //其餘insert,delete,update方法自行實現 } <!-- 配置dao實現類-- > <bean id="genericDao" class="com.xxx.MybatisDao"> <property name="sqlSessionFactory"> <ref bean="sqlSessionFactory" /> </property> <property name="batchSqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- 配置dao使用--> <bean id="myClass" class="com.xxx.MyClass"> <property name="myDao" ref="genericDao"></property> </bean> 在MyClass中是用: private MybatisDao myDao; User user = myDao.select("ssm.mapper.UserMapper.findUserById",1); 3.二、mapper方式使用 3.2.1配置xml <!-- 掃描basePackage下全部以@MyBatisDao註解的接口 --> <bean id="mapperScannerConfigurer" class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> --> <!--自動掃描包路徑--> <property name="basePackage" value="com.xxx.path"/> <!--自動掃描MyBatisDao註解標註的接口並自動裝載--> <property name="annotationClass" value="com.xxx.annotation.MyBatisDao"/> <!--<property name="markerInterface" value="com.xxx.CrudMapper"/>--> </bean> 3.2.2 //MyBatisDao 註解定義 @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Component public @interface MyBatisDao {} 3.2.3 //mapper接口,至關於dao接口,接口類名必須和mapper.xml命名空間同樣UserMapper ,結合typeAliasesPackage使用可省掉命名空間包名路徑 public interface UserMapper extends Mapper<T>, MySqlMapper<T> { //根據id查詢用戶信息,方法名必須和mapper.xml配置的id名稱一致,參數類型必須和mapper.xml配置parameterType類型一致,返回類型必須和配置的resultType一直 public User findUserById(int id) throws Exception; }