说在前面:xx这篇文章主要是参考了官网doc,大家有时间的话尽量去官网上看看,有中文的说明啊,这里把Mybatis的官网附上(额,得外网才能看哈)
引用
http://blog.mybatis.org/p/products.html
一、先附上数据库代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <description>数据库配置</description> <!-- 数据库配置文件位置 --> <!-- 配置dbcp数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${gate.driverClassName}" /> <property name="url" value="${gate.jdbcUrl}" /> <property name="username" value="${gate.user}" /> <property name="password" value="${gate.password}" /> <property name="minIdle" value="${gate.minIdle}" /> <!-- 队列中的最小等待数 --> <property name="maxIdle" value="${gate.maxIdle}" /> <!-- 队列中的最大等待数 --> <property name="maxWait" value="${gate.maxWait}" /> <!-- 最长等待时间,单位毫秒 --> <property name="maxActive" value="${gate.maxActive}" /> <!-- 最大活跃数 --> <property name="initialSize" value="${gate.initialSize}" /><!-- 初始大小 --> </bean> <!-- 定义 myBatis的sqlSessionFactory 1、当使用MapperScannerConfigurer时不需要configLocation定义( 1:mapperLocations可以批量载入mapper,但是MapperScannerConfigurer扫描mapper后不会将已存在的mapper加入到Configuration中 2:也可以定义configLocation文件,在里面设置settings和typeAliases等来覆写默认的配置 3:如果使用了configLocation文件,并且里面也定义了mappers,那么在MapperScannerConfigurer扫描动作中就不会加入已经存在的mapper了 (同mapperLocations情况一样) 4:综上所述:建议采用扫描来完成mapper的注册,并且在sqlSessionFactory的bean配置中不需要设置mapperLocations, 如果设置了configLocation文件,那么configLocation文件里也不需要再设置mapper了 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:config/sqlMapConfig.xml"></property> <!-- classpath后的*必不可少 --> <property name="mapperLocations" value="classpath*:config/mapper/*.xml"></property> <!-- 自动扫描需要定义类别名的包,将包内的JAVA类的类名作为类别名 --> <property name="typeAliasesPackage" value="com.free.bean"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 自动扫描当前包下的所有mapper,进行自动化映射,也可以通过定制化注解方式进行自动扫描annotationClass --> <property name="basePackage" value="com.free.mapper" /> </bean> <!-- 使用JDBC事物 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- AOP配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="query*" read-only="true" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 配置AOP切面 --> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.free.service.*.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config> <!-- 使用annotation注解方式配置事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
二、映射器接口代码
public interface CarGroupUrlMapper { @Select("select * from gate_cargroupurl") public List<CarGroupUrl> getCarGroupMap(); @Select("select * from gate_cargroupElse") public List<CarGroupElse> getCarGroupElse(); }
我这个目录下其实有好几个映射器文件的,不一一列举了,噢,具体的mybatis语法,以后可能会有提及吧,这里只讲下框架。
三、实体类
/** * 由于在配置文件中配置了自动别名机制(这个机制是mybatis-spring的东西,不是mybatis的别名),所以这里没有使用@Alias,如果使用mybatis * 的别名机制,此处必须写上注解才行哦 * @author xxfengxia * */ public class CarGroupUrl implements Serializable{ private int id; //分组id private String groupId; //对应的url private String url; private String md5key; }
四、映射文件说明
由于我这里采用的是自动映射和SQL注解方式,所以 当前映射器 没有配置文件,如果我们采用xml文件进行映射的话,Mybatis建议将这个文件放在mapper同级的路径下。其实这个就看大家喜好啦,我一般将这些文件都当做配置文件放在/src/config/mapper/mysql/user/user.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="com.free.mapper.RegisterMapper"> <cache type="org.mybatis.caches.ehcache.LoggingEhcache" readOnly="true" /> <select id="getRegisterInfoByCarId" parameterType="CarRegisterVO" resultType="CarRegisterVO"> select * from rent_carregister where carid = #{carid} and islogoff = 0 </select> </mapper>
五、声明:写在这里只是做一个笔记,以后查的时候方便自己查阅,不用每次都查很多资料才梳理清楚所有东西