mybatis pageHelper + 通用mapper的使用

为了加快开发速度选择使用以下插件

源码地址,包含了官方开发文档:http://git.oschina.net/free/Mapper

主要配置结构雏形,待优化

2017-09-05 20:44:30 已将项目中使用到mapper的部分精简打包,有空再更新

http://git.oschina.net/zx1323/ssm-collection

maven依赖 中添加

		<!--分页插件 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>5.0.0</version>
		</dependency>
		<!-- 通用mapper -->
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper</artifactId>
			<version>3.4.2</version>
		</dependency>

spring-core 配置

<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	default-autowire="byName"
	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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

	<context:component-scan base-package="com.ssm.collection.core" />
	<tx:annotation-driven />

	<import resource="classpath:datasource-context.xml" />

	<bean id="mybatisSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation">
			<value>classpath:spring-mybaits.xml</value>
		</property>
		<!-- mapper扫描 -->
		<property name="mapperLocations" value="classpath*:com/ssm/collection/core/**/*Mapper.xml" />
	</bean>


	<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 扫描不到实体 会报错 -->
		<property name="basePackage" value="com.ssm.collection.core.*" />
		<property name="markerInterface" value="com.ssm.collection.core.common.mapper.MyBaseMapper" />
		<property name="sqlSessionFactoryBeanName" value="mybatisSqlSessionFactory"></property>
	</bean>

	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
		<constructor-arg index="0" ref="mybatisSqlSessionFactory" />
	</bean>

	<aop:aspectj-autoproxy />

	<aop:config>
		<aop:pointcut id="appService" expression="execution(* com.ssm.collection.core.service..*Service*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="appService" />
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
		  <tx:method name="insert*" propagation="REQUIRED" />  
           <tx:method name="update*" propagation="REQUIRED" />  
		    <tx:method name="del*" propagation="REQUIRED" />  
			<tx:method name="select*" read-only="true"   propagation="SUPPORTS" />
			<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
			<tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
			<tx:method name="count*" propagation="SUPPORTS" read-only="true" />  
			<tx:method name="*" read-only="true" propagation="SUPPORTS"/>
		</tx:attributes>
	</tx:advice>


	<!-- Transactions management -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
		<qualifier value="transactionManager" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>
datasource-context.xml
<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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
						http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
						http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
	default-autowire="byName">
	
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass" value="${JDBC.ssm.Driver}" />
		<property name="jdbcUrl" value="${JDBC.ssm.URL}"/>
		<property name="user" value="${JDBC.ssm.User}" />
		<property name="password" value="${JDBC.ssm.Password}" />
		<property name="minPoolSize" value="${JDBC.ssm.MinPoolSize}" />
		<property name="maxPoolSize" value="${JDBC.ssm.MaxPoolSize}" />
		<property name="initialPoolSize" value="${JDBC.ssm.InitialPoolSize}" />
		<property name="maxIdleTime" value="${JDBC.ssm.MaxIdleTime}" />
		<property name="acquireIncrement" value="${JDBC.ssm.AcquireIncrement}" />
		<property name="acquireRetryAttempts" value="${JDBC.ssm.AcquireRetryAttempts}" />
		<property name="acquireRetryDelay" value="${JDBC.ssm.AcquireRetryDelay}" />
		<property name="idleConnectionTestPeriod" value="${JDBC.ssm.IdleConnectionTestPeriod}" />
		<property name="checkoutTimeout" value="${JDBC.ssm.CheckoutTimeout}" />
	</bean>

</beans>

system-config.properties

JDBC.Driver=com.mysql.jdbc.Driver
JDBC.URL=jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxx
JDBC.User=
JDBC.Password=
JDBC.MinPoolSize=0
JDBC.MaxPoolSize=5
JDBC.InitialPoolSize=0
JDBC.MaxIdleTime=25000
JDBC.AcquireIncrement=1
JDBC.AcquireRetryAttempts=30
JDBC.AcquireRetryDelay=1000
JDBC.TestConnectionOnCheckin=true
JDBC.AutomaticTestTable=c3p0TestTable
JDBC.IdleConnectionTestPeriod=18000
JDBC.CheckoutTimeout=0
spring-mybatisd.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="false" />
		<setting name="lazyLoadingEnabled" value="true" />
		<setting name="aggressiveLazyLoading" value="false" />
	</settings>

	<typeAliases>
		<package name="com.ssm.collection.core" />
	</typeAliases>

	<plugins>
		<plugin interceptor="com.github.pagehelper.PageInterceptor">
			<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
			 <!-- 4.0.0以后版本可以不设置该参数 -->  
			<property name="helperDialect" value="mysql" />
		</plugin>
	</plugins>

</configuration>


实体中  引用别的类当字段 需加上@Transient

===============================================

下载项目后,要修改数据库配置,导入sql 后,即可正常跑单元测试

由于项目中参杂着另外一种在mapper文件中手写sql的用法,好处是传参、返回结果时,参数都可以是map结构,不用new实体,所以做了保留

dao包下的类:是需要自己在   mapper.xml文件中写sql的实现方式(用户的批量查询,就是这种方式实现的)

service包下的类:是通用mapper的实现方式(根据user  id   查找用户,就是这种方式实现的)

entity包下就是对应数据库表的实体


BaseActionContorller:利用通用mapper插件,实现对但表的CRUD

UserController:继承BaseActionContorller后,单表CRUD基本上很容易了(实际使用中肯定会复杂很多,比如新增,更新时密码的md5加密)


平时开发基本上 只需要在   UserService      UserServiceImpl     两个类中编写代码

博文写的烂,轻喷


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值