Spring + MyBatis配置详细教程

接手的项目发现,得需要用mybatis连接本地数据库,根据本地数据库表创建sql语句。上一遍讲过mybatis的使用,本篇主要讲mybatis的配置,如下所示:

一、创建数据库及表

本次是用的Mysql数据库,语句参考上篇MYSQL。

二、maven配置mybatis框架

用maven来管理所有的架包

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>

三、用maven来配置数据连接

在maven的配置文件pom.xml文件里编写。
在maven中配置数据连接,方便以后打包(具体为什么方便打包的细节我还是不太清楚)

<profiles>
        <profile>

            <id>dev</id>
            <properties>
             <from_jdbc.url><???> </from_jdbc.url>
            <from_jdbc.user>app</from_jdbc.user>
            <from_jdbc.password>h</from_jdbc.password>

          </profile>
    </profiles>

四、编写jdbc.properties文件

jdbc.properties文件中变量值等于maven的配置文件pom.xml里的值。

from_jdbc.url=${from_jdbc.url}
from_jdbc.user=${from_jdbc.user}
from_jdbc.password=${from_jdbc.password}

五、配置spring.xml文件

//配置数据连接的读取路径
<context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${recommend_jdbc.url}" />
        <property name="username" value="${recommend_jdbc.user}" />
        <property name="password" value="${recommend_jdbc.password}" />
        <!-- 配置初始化大小-->
        <property name="initialSize" value="10" />
        <!-- 配置初始化最小 -->
        <property name="minIdle" value="1" />
        <!-- 配置初始化最大 -->
        <property name="maxActive" value="10" />

        <!-- 获取连接等待超时的时间 -->
        <property name="maxWait" value="60000" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <property name="validationQuery" value="SELECT 'x'" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="poolPreparedStatements" value="false" />
        <!-- <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/> -->
        <!--连接泄漏处理。Druid提供了RemoveAbandanded相关配置,用来关闭长时间不使用的连接。-->
        <!-- 打开removeAbandoned功能 --> 
        <property name="removeAbandoned" value="true"/>      
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="1800"/>
         <!-- 关闭abanded连接时输出错误日志 -->       
        <property name="logAbandoned" value="true"/>

        <!-- 配置监控统计拦截的filters -->      
        <!-- <property name="filters">stat,wall</property> --> 
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- cglib管理事务,控制在实现类中 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>  
</beans>





    <bean class="org.apache.ibatis.logging.LogFactory" init-method="useStdOutLogging"></bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.hiveview.entity" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath*:mapper/**/*.xml" />  
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hiveview.dao" />
    </bean>
</beans>

五、创建上层调用接口

package com.hiveview.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

@Repository
public interface BaseDao<T> {

    public int save(T t);

    public int delete(T t);

    public T get(T t);

    public List<T> getList(T t);

    public int update(T t);

    public int count(T t);

    public List<T> getPagin(T t);

    public void insertAll(List<T> list);
}

六、创建Bean类

package com.hiveview.entity.copy;

import java.util.Date;

public class CopyBean {
    private int id;
    private String fromtable;
    private String totable;
    private String fromcolumns;
    private String tocolumns;
    private int effective;
    private String executetime;
    private int isupdate;
    private String updatecolumn;
    private String columntype;
    private String fromdatasource; 
    public String getColumntype() {
        return columntype;
    }
    public void setColumntype(String columntype) {
        this.columntype = columntype;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFromtable() {
        return fromtable;
    }
    public void setFromtable(String fromtable) {
        this.fromtable = fromtable;
    }
    public String getTotable() {
        return totable;
    }
    public void setTotable(String totable) {
        this.totable = totable;
    }
    public String getFromcolumns() {
        return fromcolumns;
    }
    public void setFromcolumns(String fromcolumns) {
        this.fromcolumns = fromcolumns;
    }

    public String getTocolumns() {
        return tocolumns;
    }
    public void setTocolumns(String tocolumns) {
        this.tocolumns = tocolumns;
    }
    public int getEffective() {
        return effective;
    }
    public void setEffective(int effective) {
        this.effective = effective;
    }
    public String getExecutetime() {
        return executetime;
    }
    public void setExecutetime(String executetime) {
        this.executetime = executetime;
    }
    public int getIsupdate() {
        return isupdate;
    }
    public void setIsupdate(int isupdate) {
        this.isupdate = isupdate;
    }
    public String getUpdatecolumn() {
        return updatecolumn;
    }
    public void setUpdatecolumn(String updatecolumn) {
        this.updatecolumn = updatecolumn;
    }
    public String getFromdatasource() {
        return fromdatasource;
    }
    public void setFromdatasource(String fromdatasource) {
        this.fromdatasource = fromdatasource;
    }



}

七、编写mapper.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.hiveview.dao.copy.CopyDao">
    <select id="getList" parameterType="com.hiveview.entity.copy.CopyBean"
        resultType="com.hiveview.entity.copy.CopyBean">
        select * from copy_table  where 1=1
        <if test="fromtable!=null and fromtable!=''">
            and fromtable=#{fromtable}
        </if>
    </select>
    <update id="update" parameterType="com.hiveview.entity.copy.CopyBean">
        update copy_table set executetime=#{executetime}
    </update>
</mapper>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值