码迷,mamicode.com
首页 > 编程语言 > 详细

《Java Spring框架》通过Idea 整合Spring和Mybatis

时间:2020-01-04 20:27:43      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:app   vat   ado   depend   action   count()   odi   nfa   data   

1. Jar包下载

百度云下载:链接: https://pan.baidu.com/s/1sVUovsBfj8NWdthGIbyqGA 提取码: 8v3u 复制这段内容后打开百度网盘手机App,操作更方便哦

2 通过IDEA整合

第一步:新增项目

技术图片

第二步:不通过Gradle也可以的,选好java 和 web 点击下一步。

技术图片

第三步:取个名字

技术图片

第四步:设置Gradle 和 JDK版本,当然没有gradle也不影响的。

技术图片

第五步:新增lib文件,用于存放下载的jar包,也可以通过Gradle下载。

技术图片

第六步:build.gradle文件 ,引入jar包。

group ‘ssm‘
version ‘1.0-SNAPSHOT‘

apply plugin: ‘java‘
apply plugin: ‘war‘

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: ‘junit‘, name: ‘junit‘, version: ‘4.12‘
    compile files(‘lib/c3p0-0.9.5.2.jar‘)
    compile files(‘lib/com.springsource.org.aopalliance-1.0.0.jar‘)
    compile files(‘lib/com.springsource.org.apache.commons.logging-1.1.1.jar‘)
    compile files(‘lib/com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar‘)
    compile files(‘lib/mchange-commons-java-0.2.11.jar‘)
    compile files(‘lib/mysql-connector-java-5.1.46-bin.jar‘)
    compile files(‘lib/ojdbc7.jar‘)
    compile files(‘lib/spring-aop-5.0.8.RELEASE.jar‘)
    compile files(‘lib/spring-aspects-5.0.8.RELEASE.jar‘)
    compile files(‘lib/spring-beans-5.0.8.RELEASE.jar‘)
    compile files(‘lib/spring-context-5.0.8.RELEASE.jar‘)
    compile files(‘lib/spring-core-5.0.8.RELEASE.jar‘)
    compile files(‘lib/spring-expression-5.0.8.RELEASE.jar‘)
    compile files(‘lib/spring-jdbc-5.0.8.RELEASE.jar‘)
    compile files(‘lib/spring-tx-5.0.8.RELEASE.jar‘)
    compile files(‘lib/mybatis-3.4.6.jar‘)
    compile files(‘lib/mybatis-spring-1.3.2.jar‘)
    compile group: ‘org.springframework‘, name: ‘spring-test‘, version: ‘5.0.8.RELEASE‘
}

跑了测试案例:

数据库表数据如下:

技术图片

目录结构:

技术图片

/**
 * 账户
 * @author hubt
 */
public class Account {
    private Integer id;
    private String name;
    private Double money;
    
    //转账金额
    private Double tranferMoney;
    public Double getTranferMoney() {
        return tranferMoney;
    }
    public void setTranferMoney(Double tranferMoney) {
        this.tranferMoney = tranferMoney;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Double getMoney() {
        return money;
    }
    public void setMoney(Double money) {
        this.money = money;
    }
}
import com.tao.bean.Account;

/**
 * 账户mapper接口
 * @author Joey
 *
 */
public interface AccountMapper {
    //操作数据库扣款和加款
    
    //扣款
    void subMoney(Account pay);
    
    
    //加款
    void addMoney(Account collect);
}
/**
 * 账户接口
 * @author hubt
 *
 */
public interface AccountService {
    
    //转账方法
    void updateTranferAccount();
}
import javax.annotation.Resource;

import com.tao.bean.Account;
import com.tao.mapper.AccountMapper;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(isolation= Isolation.DEFAULT, propagation= Propagation.REQUIRED, readOnly=false)
public class AccountServiceImpl implements AccountService {
    
    @Resource(name = "accountMapper")
    private AccountMapper mapper;

    @Override
    public void updateTranferAccount() {
        Double tranferMoney = 1000d;
        Account pay = new Account();
        pay.setId(1);
        pay.setTranferMoney(tranferMoney);
        //先扣款
        mapper.subMoney(pay);
        Account collect = new Account();
        collect.setId(2);
        collect.setTranferMoney(tranferMoney);
        //加款
        mapper.addMoney(collect);
    }
}
import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.tao.service.AccountService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MapperTest {

    @Resource(name="accountService")
    private AccountService as;

    @Test
    public void Test1() {
        as.updateTranferAccount();
    }
}

配置文件:applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
        
    <!-- 读取配置文件 -->
    <context:property-placeholder location="db.properties"/>
    
    <!-- 配置 dataSource -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- mybatis -->
    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
    </bean>
    
    <!-- mapper工厂 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.tao.mapper"/>
    </bean>
    
    <!-- service -->
    <bean name="accountService" class="com.tao.service.AccountServiceImpl"></bean>

    <!-- 需要事务核心管理器 -->
    <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 开启注解事务 -->
    <tx:annotation-driven/>
    
</beans>

配置文件:db.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/work_db
jdbc.user=root
jdbc.password=hbt106887

sqlMapConfig.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>
      <typeAliases>
          <package name="com.tao.bean"/>
      </typeAliases>
  </configuration>

运行结果:

技术图片

总结:就这样的完美。

《Java Spring框架》通过Idea 整合Spring和Mybatis

标签:app   vat   ado   depend   action   count()   odi   nfa   data   

原文地址:https://www.cnblogs.com/jssj/p/12149540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!