1 Star 0 Fork 0

hoaven / hoaven-libMybatis

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

一、简介

对tk.mybatis的封装,旨在简化对数据库的操作。

二、使用

1、数据源配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="aliAdminDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
          destroy-method="close">
        <!-- 基本属性 url、user、password -->
        <property name="url" value="${aliAdminUrl}"/>
        <property name="username" value="${aliAdminUser}"/>
        <property name="password" value="${aliAdminPassword}"/>
        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="5"/>
        <property name="minIdle" value="2"/>
        <property name="maxActive" value="300"/>

        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000"/>
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000"/>
        <property name="validationQuery" value="SELECT 1"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>

        <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="false"/>
        <!--  <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />-->
        <!-- 配置监控统计拦截的filters,wall-sql攻击 -->
        <property name="filters" value="mergeStat,wall"/>
        <!-- 慢查询slowSqlMillis的缺省值为3000,也就是3秒 -->
        <property name="connectionProperties" value="druid.stat.slowSqlMillis=3000"/>
        <property name="removeAbandoned" value="true"/> <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandonedTimeout" value="1800"/> <!-- 1800秒,也就是30分钟 -->
        <property name="logAbandoned" value="true"/> <!-- 关闭abanded连接时输出错误日志 -->
    </bean>

    <!-- myBatis文件 -->
    <bean id="aliAdminSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" lazy-init="true">
        <property name="dataSource" ref="aliAdminDataSource"/>
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageHelper">
                    <property name="properties">
                        <value>
                            dialect=mysql
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <bean name="aliAdminSqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="aliAdminSqlSessionFactory"/>
    </bean>

    <!-- 配置MyBatis Mapper路径 -->
    <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hoaven.aliadmin.mapper"/>
        <property name="markerInterface" value="com.cashbus.mybatis.GenericMapper"/>
        <property name="sqlSessionFactoryBeanName" value="aliAdminSqlSessionFactory"/>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="aliAdminTXManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="aliAdminDataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="aliAdminTXManager"/>

</beans>

2、通用dao

@Repository
public class AliAdminBaseDao extends GenericDao {
    @Resource
    SqlSession aliAdminSqlSession;

    @Override
    protected SqlSession getSession() {
        return aliAdminSqlSession;
    }

    @Override
    public String getBasePackage() {
        return "com.hoaven.aliadmin.mapper";
    }
}

3、通用service

public interface IAliAdminBaseService extends IGenericService{
}

@Service
@Transactional(value = "aliAdminTXManager")
public class AliAdminBaseService extends GenericService implements IAliAdminBaseService {
    @Resource
    AliAdminBaseDao aliAdminBaseDao;

    @Override
    public GenericDao getGenericDao() {
        return aliAdminBaseDao;
    }
}

4、model定义

/**
 * Created by hoaven on 2017/8/19.
 */
@Table(name = "user")
@Data
@NoArgsConstructor
@AllArgsConstructor
@NameStyle
public class User extends GenericModel {
    private String username;
    private String password;
    private String token;
    private Date lastLoginTime;
    private String verifyStatus;
    private Date verifyTime;
    private String handImage;

}

5、mapper定义

/**
 * Created by hoaven on 2017/8/19.
 */
public interface UserMapper extends GenericMapper<User> {
}

5、操作DB示例

   User originUser = aliAdminBaseService.singleBySQL("where username = #{0}", User.class, param.getUsername());

空文件

简介

对tk.mybatis的封装,旨在简化对数据库的操作。 展开 收起
Java
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/kakanshun/hoaven-libMybatis.git
git@gitee.com:kakanshun/hoaven-libMybatis.git
kakanshun
hoaven-libMybatis
hoaven-libMybatis
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891