一种java工程整合mybatis

本文详细介绍了MyBatis框架的使用方法,包括Service层、Dao层的实现,XML配置文件的解析,以及MyBatisUtils工具类的创建。通过具体的代码示例,展示了如何在Java项目中集成MyBatis进行数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Service
    public void 执行方法(参数){
        try {
            sqlSession = MybatisUtils.getSqlSessionFactory().openSession();
            Dao层= sqlSession.getMapper(Dao层.class);
            执行方法
            sqlSession.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            sqlSession.rollback();
        }finally{
            sqlSession.close();
        }
    }

2.Dao

public void 方法名称(参数名称);

3.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="Dao全路径">

  
    <insert id="方法名称" parameterType="形参">
       SQL语句
    </insert>
</mapper>

4.MybatisUtil

 

package com.jinhui.jhrobotcon.utils;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


/**
 * 2015-11-16下午05:34:31
 * MybatisUtils.java
 * author:liuyonghua
 */
public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory;
    
    private static SqlSessionFactory sqlCentralSessionFactory;

    
    public static SqlSessionFactory getSqlSessionFactory() {
        if(sqlSessionFactory==null){
             String resource = "mybatis-config.xml";
             Reader reader = null;
             try {
                 reader = Resources.getResourceAsReader(resource);
             } catch (IOException e) {
                 System.out.println(e.getMessage());

             }
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        }
//         System.out.println("sqlSessionFactory--------------"+sqlSessionFactory);
        return sqlSessionFactory;
    }
    
    public static SqlSessionFactory getCentralSqlSessionFactory() {
        if(sqlCentralSessionFactory==null){
             String resource = "mybatis-config.xml";
             Reader reader = null;
             try {
                 reader = Resources.getResourceAsReader(resource);
             } catch (IOException e) {
                 System.out.println(e.getMessage());

             }
             sqlCentralSessionFactory = new SqlSessionFactoryBuilder().build(reader, "developmentCentral");
        }
//         System.out.println("sqlSessionFactory--------------"+sqlSessionFactory);
        return sqlCentralSessionFactory;
    }
    
    public static void main(String[] args) {
        SqlSession sqlSession = null;
        try {
//            sqlSession = MybatisUtils.getSqlSessionFactory().openSession();
//            IIcpGnBaxxSlxxViewDao icpGnBaxxSlxxViewDao = sqlSession.getMapper(IIcpGnBaxxSlxxViewDao.class);
//            Map<String, String> map = new HashMap<String, String>();
//            map.put("YM", "wanlitech.com");
//            IcpGnBaxxSlxxViewBean iBean = icpGnBaxxSlxxViewDao.getIcpBeiAn(map);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            sqlSession.close();
        }
    }
}

5.配置文件

<?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>
    <properties resource="jdbc.properties"/>
    <settings>   
        <!-- 全局的映射器启用或禁用缓存。 -->
        <setting name="cacheEnabled" value="true"/>  
        <!-- 全局启用或禁用延迟加载 -->
        <setting name="lazyLoadingEnabled" value="true"/>  
        <!-- 允许或不允许多种结果集从一个单独的语句中返回 -->
        <setting name="multipleResultSetsEnabled" value="true"/>  
        <setting name="useColumnLabel" value="true"/>  
        <setting name="useGeneratedKeys" value="true"/>  
        <setting name="defaultExecutorType" value="SIMPLE"/>  
         <!-- 设置超时时间 -->
        <setting name="defaultStatementTimeout" value="25000"/>
        <setting name="logImpl" value="LOG4J" />     
    </settings>   
     <typeAliases>   
      
     
    </typeAliases>     
    <environments default="development">   
       <environment id="development">   
           <transactionManager type="jdbc"/>   
           <dataSource type="POOLED">   
              <property name="driver" value="${jdbc.driver}"/>  
              <property name="url" value="${jdbc.url}"/>  
              <property name="username" value="${jdbc.username}"/>  
              <property name="password" value="${jdbc.password}"/>  
              <property name="poolMaximumActiveConnections" value="30" />
              <property name="poolTimeToWait" value="5000" />
              <property name="poolPingEnabled" value="true"/>
              <property name="poolPingQuery" value="select 1 from dual" />   
           </dataSource>   
       </environment>
      
    </environments>
    <mappers>
     
        <mapper resource="com/jinhui/jhrobotcon/mybatis/StoppointPath.xml" />
    </mappers>
</configuration> 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值