mybatis的使用
1.配置依赖
<!-- mybatis start --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency>
2.配置数据源
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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/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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <aop:aspectj-autoproxy /> <tx:annotation-driven /> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/yun" /> <property name="username" value="root" /> <property name="password" value="centos" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="1" /> <property name="minIdle" value="1" /> <property name="maxActive" value="10" /> <!-- 配置获取连接等待超时的时间 10秒 --> <property name="maxWait" value="10000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="testWhileIdle" value="true" /> <!-- 这里建议配置为TRUE,防止取到的连接不可用 --> <property name="testOnBorrow" value="true" /> <property name="testOnReturn" value="false" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 这里配置提交方式,默认就是TRUE,可以不用配置 --> <property name="defaultAutoCommit" value="true" /> <!-- 验证连接有效与否的SQL,不同的数据配置不同 --> <property name="validationQuery" value="select 1 from dual " /> <property name="filters" value="log4j" /> <property name="proxyFilters"> <list> <!-- web 监控方式 --> <!-- <ref bean="stat-filter" /> --> <!-- 日志文件监控方式 --> <ref bean="logFilter" /> </list> </property> </bean> <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter"> <property name="mergeSql" value="true" /> <property name="slowSqlMillis" value="10000" /> <property name="logSlowSql" value="true" /> </bean> <bean id="logFilter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter"> <property name="statementExecutableSqlLogEnable" value="false" /> </bean> <!-- jdbc事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <!-- 配置事物传播特性,(REQUIRED)是否存在事务,不存在则创建一个事务 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="merge*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="query*" read-only="true"/> <tx:method name="use*" read-only="true"/> <tx:method name="get*" read-only="true" /> <tx:method name="count*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="list*" read-only="true" /> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 切面,配置参与事务的类 --> <aop:config > <aop:pointcut id="allMethod" expression="execution(* com.qi.base.service.impl.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod" /> </aop:config> </beans>
2.配置mybatis,指定接口及接口实现
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" default-lazy-init="false" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 2. mybatis的SqlSession的工厂--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:com/qi/base/dao/impl/*.xml" /> </bean> <!-- 3. mybatis自动扫描加载Sql接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.qi.base.dao"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
3.写接口 放在目录 com.qi.base.dao
package com.qi.base.dao; import java.util.List; import org.springframework.stereotype.Repository; import com.qi.base.domain.MenuInfo; @Repository public interface MenuInfoDao { List<MenuInfo> queryMenuInfoList(); }
4.写映射,存放目录为 com/qi/base/dao/impl
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <!-- MenuInfo数据接口对应的SQL【 v1.0 2017-03-04 创建】--> <mapper namespace="com.qi.base.dao.MenuInfoDao"> <!-- 菜单信息与实体表菜单信息对应关系 --> <resultMap id="MenuInfoRM" type="com.qi.base.domain.MenuInfo" > <result property="id" column="id" /> <!-- 菜单ID --> <result property="parentId" column="parent_id" /> <!-- 父级菜单ID --> <result property="menuName" column="menu_name" /> <!-- 菜单名称 --> <result property="menuUrl" column="menu_url" /> <!-- 菜单地址 --> <result property="menuType" column="menu_type" /> <!-- 1:主菜单 2:子菜单 --> <result property="menuStatus" column="menu_status" /> <!-- 菜单状态,1:正常,2:失效 --> <result property="createTime" column="create_time" /> <!-- 修改时间 --> <result property="updateTime" column="update_time" /> <!-- 更新时间 --> </resultMap> <select id="queryMenuInfoList" resultMap="MenuInfoRM"> select * from menu_info where 1=1 </select> </mapper>
ok ,可以启动测试了