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="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>
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="${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> 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="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;
}

  

mybatis使用Dao和Mapper方式的更多相关文章

  1. mybatis开发Dao的Mapper动态代理方式

    1. 开发规范Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体跟Dao原始方法中接口实现类的方法相 ...

  2. mybatis的dao的mapper写法

    ## MyBatis的Dao编写[mapper代理方式实现] step1: 写一个接口,并写抽象方法 package com.sjl.mapper; import com.sjl.model.User ...

  3. Mybatis的Dao向mapper传多个参数(三种解决方案)转自《super超人》

    第一种方案 : DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id=" ...

  4. Mybatis的Dao向mapper传多个参数(三种解决方案)

    第一种方案 : DAO层的函数方法 Public User selectUser(String name,String area); 对应的Mapper.xml <select id=" ...

  5. mybatis的dao向mapper.xml传入多参数

    https://www.cnblogs.com/super-chao/p/7722411.html 如果两种不同类型的参数传入,parameterType可以不写,直接获取#{0},#{1}就可以传入 ...

  6. MyBatis开发Dao层的两种方式(Mapper动态代理方式)

    MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发) 接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式 Ma ...

  7. mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类

    相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...

  8. mybatis自动生成service、dao、mapper

    1.config.properties ## 数据库连接参数 jdbc.driverClass = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://loca ...

  9. MyBatis开发Dao层的两种方式(原始Dao层开发)

    本文将介绍使用框架mybatis开发原始Dao层来对一个对数据库进行增删改查的案例. Mapper动态代理开发Dao层请阅读我的下一篇博客:MyBatis开发Dao层的两种方式(Mapper动态代理方 ...

随机推荐

  1. AI-sklearn 学习笔记(二)数据集

    from sklearn import datasets from sklearn.linear_model import LinearRegression loaded_data = dataset ...

  2. log4j常用的配置文件

    # priority :debug<info<warn<error #you cannot specify every priority with different file fo ...

  3. 阿里P7前端需要哪些技能

    原谅我copy过来的,但是这个条理很清楚很有借鉴意义 前言 以下是从公众号的文章中获取到的一位阿里的前端架构师整理的前端架构p7的技能图谱,当然不是最完整.最系统的,所以之后我会一直维护更新这里的内容 ...

  4. 基于Xilinx Kintex-7 XC7K325T 的FMC USB3.0 SATA 四路光纤数据转发卡

    基于Xilinx Kintex-7 XC7K325T 的FMC USB3.0 SATA 四路光纤数据转发卡 1. 板卡概述 本板卡基于Xilinx公司的FPGAXC7K325T-2FFG900 芯片, ...

  5. shell 搜索指定目录下所有 jar 文件生成csv文件

    虽说比较简单,但希望分享给大家.按需求改成想找的:例如txt,xls 等. 脚本名 扫描的路径 文件名 testFind.sh /  testFind.txt (如果未配置环境变量  ./testFi ...

  6. MongoDB的环境搭建及启动

    MongoDB环境搭建及配置 一.环境搭建 Mac:brew install mongodb 常见问题: Error: Permission denied @ unlink_internal 解决方案 ...

  7. 06Web服务

    1.web开发入门 1.1 引入 软件结构分类: CS结构:客户端和服务器端 特点: 1)必须安装特点的客户端程序 2)服务器端升级,客户端同步升级 BS结构:浏览器和服务器端 特点: 1)不需要安装 ...

  8. 64bit assembly

    16个通用寄存器: %rax %rbx %rcx %rdx %rsi %rdi %rbp %rsp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15

  9. css3 first-of-type选择器以及css3选择器中:first-child与:first-of-type的区别

    CSS3 first-of-type选择器 “:first-of-type”选择器类似于“:first-child”选择器,不同之处就是指定了元素的类型,其主要用来定位一个父元素下的某个类型的第一个子 ...

  10. Centos 的防火墙(firewalld,iptables)

    Centos系统防火墙介绍 概述: 1.Filewalld(动态防火墙)作为redhat7系统中变更对于netfilter内核模块的管理工具: 2.iptables service 管理防火墙规则的模 ...