1)创建一个spring-mybatis-mysql这么一个javaweb或java工程

2)导入spring-ioc,spring-aop,spring-transaction,mybatis,c3p0,mysql/oracle相关的jar包和spring整合mybatis的jar包

3)创建students.sql

--mysql
create table students(
sid int(5) primary key,
sname varchar(10),
ssal double(8,2)
);

4)创建Student.java

/**
* 学生*/
public class Student {
private Integer id;//编号
private String name;//姓名
private Double sal;//薪水
public Student(){}
public Student(Integer id, String name, Double sal) {
this.id = id;
this.name = name;
this.sal = sal;
}
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 getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
}

5)创建StudentMapper.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="studentNamespace">
<resultMap type="loaderman.entity.Student" id="studentMap">
<id property="id" column="sid" />
<result property="name" column="sname"/>
<result property="sal" column="ssal"/>
</resultMap>
<insert id="insert" parameterType="loaderman.entity.Student">
insert into students(sid,sname,ssal) values(#{id},#{name},#{sal})
</insert>
</mapper>

6)创建StudentDao.java

public class StudentDao {
private SqlSessionFactory sqlSessionFactory;
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public void insert(Student student){
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.insert("studentNamespace.insert",student);
//int i = 10/0;
}
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});
StudentDao studentDao = (StudentDao) ac.getBean("studentDaoID");
studentDao.insert(new Student(1,"哈哈",7000D));
}
}

7)在src目录下创建mybatis.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>
<mappers>
<mapper resource="loaderman/entity/StudentMapper.xml"/>
</mappers>
</configuration>

8)在src目录下创建spring.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:mvc="http://www.springframework.org/schema/mvc" 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/aop
http://www.springframework.org/schema/aop/spring-aop-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!-- 配置C3P0连接池(即管理数据库连接) -->
<bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/mybatis"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean> <!-- 配置SqlSessionFactoryBean(即替代MyBatisUtil工具类的作用) -->
<bean id="sqlSessionFactoryBeanID" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="comboPooledDataSourceID"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean> <!-- 配置事务管理器(即使用JDBC事务管理器) -->
<bean id="dataSourceTransactionManagerID" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="comboPooledDataSourceID"/>
</bean> <!-- 配置事务通知(即哪些方法需要事务) -->
<tx:advice id="tx" transaction-manager="dataSourceTransactionManagerID">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <!-- 配置事务切面(即哪些包中的类需要事务通知) -->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* loaderman.dao.*.*(..))"/>
<aop:advisor advice-ref="tx" pointcut-ref="pointcut" />
</aop:config> <!-- 配置StudentDao类 -->
<bean id="studentDaoID" class="loaderman.dao.StudentDao">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBeanID"/>
</bean> </beans>

spring + mybatis + mysql/oracle开发的更多相关文章

  1. Spring+Mybatis+Mysql搭建分布式数据库访问框架

    一.前言 用Java开发企业应用软件, 经常会采用Spring+MyBatis+Mysql搭建数据库框架.如果数据量很大,一个MYSQL库存储数据访问效率很低,往往会采用分库存储管理的方式.本文讲述如 ...

  2. SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现

    上一章我们把SSM项目结构已搭建(SSM框架web项目从零开始--分布式项目结构搭建)完毕,本章将实现Spring,mybatis,mysql等相关配置. 1.    外部架包依赖引入 外部依赖包引入 ...

  3. Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建

    目录 Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建 0.项目准备 1.数据持久层Mybatis+MySQL 1.1 MySQL数据准备 1.2 Mybatis ...

  4. Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合

    Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合 标签: mybatisSpringbeanApplicationContextMapper 2015-12-31 1 ...

  5. freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建

    今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven ...

  6. springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

    @_@ 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) myba ...

  7. maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

    转自:https://www.cnblogs.com/lmei/p/7190755.html?utm_source=itdadao&utm_medium=referral @_@ 写在最前 之 ...

  8. SpringMVC+Spring+Mybatis+Mysql项目搭建

    眼下俺在搭建一个自己的个人站点玩玩.一边练习.一边把用到的技术总结一下,日后好复习. 站点框架大致例如以下图所看到的: 眼下仅仅用到了SpringMVC+Spring+Mybatis+Mysql.把它 ...

  9. 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解

    http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...

随机推荐

  1. unomp 矿池运行问题随记

    经过大量的实践,遇到的问题或经验如下: 1.单机运行多矿池时,单机CPU核心数 成为性能瓶颈,运行两个月后,有部分用户反映 矿机速率只有以前的一半. 2.Dash 等可以自行报块的矿池,每个块的股份比 ...

  2. 【JOISC2012】fish

    Description 有 \(n\) 条鱼,第 \(i\) 条鱼的长度为 \(L_i\),颜色是 \(C_i\)(\(C_i\) 只能是 'R','G','B'). 你需要从中挑出至少一条鱼,要求挑 ...

  3. 一个 TCP 连接可以发多少个 HTTP 请求?

    曾经有这么一道经典面试题:从 URL 在浏览器被被输入到页面展现的过程中发生了什么?相信大多数准备过的同学都能回答出来,但是如果继续问:收到的 HTML 如果包含几十个图片标签,这些图片是以什么方式. ...

  4. mysql5.7备份

    一.备份准备&备份测试 1.备份目录准备 #mysql专用目录 mkdir /mysql #mysql备份目录 mkdir /mysql/backup #mysql备份脚本 mkdir /my ...

  5. BZOJ 3489: A simple rmq problem (KD-tree做法)

    KD树水过这道可持久化树套树-其实就是个三维偏序 题解戳这里 CODE #include <bits/stdc++.h> using namespace std; #define ls ( ...

  6. Codeforces Round #587 (Div. 3) B. Shooting(贪心)

    链接: https://codeforces.com/contest/1216/problem/B 题意: Recently Vasya decided to improve his pistol s ...

  7. 【Java-算法】 计算十六进制校验位

    如何计算16进制校验位?校验的实质是16进制取和模256的值. eg. 十六进制串:0A0B0C0D0E0F 的校验位是: 4B (HEX)计算过程: 十六进制转十进制 0A (HEX)= 10(DE ...

  8. python基于opencv实现人脸定位

    import cv2 # 读取图片 img = cv2.imread("image.jpg") # 加载模型,模型可以从https://github.com/opencv/open ...

  9. [Python自学] day-21 (2) (Cookie、FBV|CBV装饰器)

    一.什么是Cookie 1.什么是Cookie? Cookie是保存在客户端浏览器中的文件,其中记录了服务器让浏览器记录的一些键值对(类似字典). 当Cookie中存在数据时,浏览器在访问网站时会读取 ...

  10. MFC 列表控件List Control

    一般用带有表头的报表模式,所以属性:View设为Report // OnInitDialog()中 CString str[] = {TEXT("姓名"),TEXT("性 ...