mybatis使用Dao和Mapper方式

满堂花醉三千客,一剑霜寒十四州。这篇文章主要讲述mybatis使用Dao和Mapper方式相关的知识,希望能为你提供帮助。
1、配置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.xml

< ?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="https://www.songbingjia.com/android/true"/> < !-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 --> < setting name="lazyLoadingEnabled" value="https://www.songbingjia.com/android/true"/> < !-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 --> < setting name="aggressiveLazyLoading" value="https://www.songbingjia.com/android/true"/> < !-- 是否允许单条sql 返回多个数据集(取决于驱动的兼容性) default:true --> < setting name="multipleResultSetsEnabled" value="https://www.songbingjia.com/android/true"/> < !-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true --> < setting name="useColumnLabel" value="https://www.songbingjia.com/android/true"/> < !-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。default:false--> < setting name="useGeneratedKeys" value="https://www.songbingjia.com/android/false"/> < !-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射 PARTIAL:部分FULL:全部--> < setting name="autoMappingBehavior" value="https://www.songbingjia.com/android/PARTIAL"/> < !-- 这是默认的执行类型(SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新)--> < setting name="defaultExecutorType" value="https://www.songbingjia.com/android/SIMPLE"/> < !-- 使用驼峰命名法转换字段。 --> < setting name="mapUnderscoreToCamelCase" value="https://www.songbingjia.com/android/true"/> < !-- 设置本地缓存范围 session:就会有数据的共享statement:语句范围 (这样就不会有数据的共享 ) defalut:session --> < setting name="localCacheScope" value="https://www.songbingjia.com/android/SESSION"/> < !-- 设置但JDBC类型为空时,某些驱动程序 要指定值,default:OTHER,插入空值时不需要指定类型 --> < setting name="jdbcTypeForNull" value="https://www.songbingjia.com/android/NULL"/> < /settings> < !-- 类型别名 --> < typeAliases> < typeAlias alias="Page" type="com.yueqiu8.common.persistence.Page" /> < !--分页--> < /typeAliases> < !-- 插件配置 --> < plugins> < !--< 页面拦截器插件/> --> < plugin interceptor="com.github.pagehelper.PageHelper" /> < /plugins> < /configuration>


2、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="https://www.songbingjia.com/android/${jdbc.driver}" />
< property name="url" value="https://www.songbingjia.com/android/${jdbc.url}" />
< property name="username" value="https://www.songbingjia.com/android/${jdbc.username}" />
< property name="password" value="https://www.songbingjia.com/android/${jdbc.password}" />
< property name="maxActive" value="https://www.songbingjia.com/android/10" />
< property name="maxIdle" value="https://www.songbingjia.com/android/5" />
< /bean>
< !--数据源配置sqlSessionFactory -->
< bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
< property name="dataSource" ref bean="dataSource"/>
< property name="typeAliasesPackage" value="https://www.songbingjia.com/android/com.xxx.path"/>
< property name="typeAliasesSuperType" value="https://www.songbingjia.com/android/xxx.xxx"/>
< property name="mapperLocations" value="https://www.songbingjia.com/android/classpath:/mappings/**/*.xml"/>
< property name="configLocation" value="https://www.songbingjia.com/android/classpath:/mybatis-config.xml"> < /property>
< /bean>
【mybatis使用Dao和Mapper方式】配置sql,UserMapper.xml供service使用
< mapper namespace="ssm.mapper.UserMapper">
< select id="findUserById" parameterType="int" resultType="user">
select * from user where id = #{id}
< /select>
< /mapper>
3、dao方式和mapper方式使用mybatis
在context-spring.xml配置不同的加载方式
3.1、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.2、mapper方式使用
3.2.1配置xml
< !-- 扫描basePackage下所有以@MyBatisDao注解的接口 -->
< bean id="mapperScannerConfigurer" class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
< !--< property name="sqlSessionFactoryBeanName" value="https://www.songbingjia.com/android/sqlSessionFactory" /> -->
< !--自动扫描包路径-->
< property name="basePackage" value="https://www.songbingjia.com/android/com.xxx.path"/>
< !--自动扫描MyBatisDao注解标注的接口并自动装载-->
< property name="annotationClass" value="https://www.songbingjia.com/android/com.xxx.annotation.MyBatisDao"/>
< !--< property name="markerInterface" value="https://www.songbingjia.com/android/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;
}

    推荐阅读