1) Create a spring-mybaits-oracle such a javaweb or Java project
2) Import the spring-integrated plug-in packages provided by Spring,mybatis,c3p0,oracle and MyBatis
MySQL Jar:mysql-connector-java-5.1.7-bin.jar Oracle Jar:ojdbc5.jar C 3p0 Jar:c3p0-0.9.1.2.jar mybatis Jar:asm-3.3.1.jar cglib-2.2.2.jar commons-logging-1.1.1.jar m Ybatis-3.1.1.jar MyBatis The jar:org.springframework of Spring's IOC module with spring integrated jar "Mybatis-spring-1.1.1.jar". Asm-3.0.5.release.jar Org.springframework.beans-3.0.5.release.jar Org.springframework.context-3.0.5.releas E.jar Org.springframework.core-3.0.5.release.jar Org.springframework.expression-3.0.5.release.jar Commons-logging.jar Spring's AOP module's Jar:aopalliance.jar aspectjweaver.jar Cglib-2.2.2.jar Org.springframework.aop-3.0.5.release.jar Spring's Transaction module Jar:org.springframework.jdbc-3.0.5.release.jar Org.springframework.orm-3.0.5.release.jar Org.springframework.transaction-3.0.5.release.jar
3) Create a emps.sql table using Oracle or MySQL syntax
--oraclecreate table Emps (Eid number (5) Primary key,ename VARCHAR2 (a), esal number (8,2), Esex varchar2 (2));
4) Create Emp.java class
Package cn.buaa.javaee.entity;/** * Employee * @author ADMINTC */public class Emp {private Integer id;private String Name;privat E Double sal;private string sex;public emp () {}public emp (Integer ID, string name, Double sal, string sex) {this.id = id;th Is.name = Name;this.sal = Sal;this.sex = sex;} 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;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;}}
5) Create a empmapper.xml mapping file
<?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= "Empnamespace" ><resultmap type= "emp" id= "Empmap" ><id property= "id" column= "eid"/> <result property= "name" column= "ename"/><result property= "Sal" column= "esal"/><result property= "Sex" Column= "Esex"/></resultmap><!--increase employee--><insert id= "Add" parametertype= "emp" >insert into Emps ( Eid,ename,esal,esex) VALUES (#{id},#{name},#{sal},#{sex}) </insert></mapper>
6) Create the Mybatis.xml configuration file, note that because spring is configured for connection pooling management, the connection pool configuration in MyBatis is removed (below for testing, not deleted, not deleted, the default is the boss Spring)
<?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 file under load Classpath--<properties resource= "db.properties"/> <!-- Set type alias--<typealiases > <typealias type= "cn.buaa.javaee.entity.Emp" alias= "Emp"/> </typeAliases> <!--Set the default connection environment information--<environments default= "Oracle_developer" > <!--connect the environment information, a unique name--<environment id= "Mysql_developer" > <!--MyBatis using JDBC things management--<transactionmanager type= "JDBC" ></transactionma Nager> <!--MyBatis Use connection pooling to get connected--<datasource type= "pooled "> <property name=" Driver "value=" Com.mysql.jdbc.Driver "/> <property name= "url" value= "Jdbc:mysql://localhost:3306/mybatis"/> <property name= "username" value= "root"/> <property name= "password" value= "1 23456 "/> </dataSource> </environment> <!--connect the environment with a unique name--<environment id= "Oracle_developer" > < !--MyBatis using JDBC things management--<transactionmanager type= "JDBC" ></transactionManager> <!--MyBatis Use connection pooling to get connected--<datasource type= "Pooled" > <property name= "Driver" value= "Oracle.jdbc.driver.OracleDriver"/> <property name= "url" value= "Jdbc:oracle:thin: @localhost: 1521/orcl"/> <prop Erty name= "username" valUe= "Zhangdong"/> <property name= "password" value= "123456"/> </dataSource> </environment> </environments> <!--load map files-- <mappers> <mapper resource= "Cn/buaa/javaee/entity/empmapper.xml"/> </mappers>< ;/configuration>
7) Create Empdao.java class
Package Cn.buaa.javaee.dao;import Org.apache.ibatis.session.sqlsession;import Org.apache.ibatis.session.sqlsessionfactory;import Cn.buaa.javaee.entity.emp;public class EmpDao {private Sqlsessionfactory SQLSESSIONFACTORY;//IOC container automatically injects public void Setsqlsessionfactory (sqlsessionfactory sqlsessionfactory) {this.sqlsessionfactory = sqlsessionfactory;} /** * Add employee */public void Add (EMP emp) throws Exception{/*sqlsession sqlsession = null;try {sqlsession = Mybatisutil.getsql Session (); Sqlsession.insert ("Empnamespace.add", EMP); Sqlsession.commit ();} catch (Exception e) {e.printstacktrace (); Sqlsession.rollback (); throw e;} Finally{mybatisutil.closesqlsession ();} */sqlsession sqlsession = sqlsessionfactory.opensession () sqlsession.insert ("Empnamespace.add", EMP); Sqlsession.close ();}}
8) Unit Test
Package Cn.buaa.javaee.test;import Org.junit.test;import Org.springframework.context.support.classpathxmlapplicationcontext;import Cn.buaa.javaee.dao.empdao;import cn.buaa.javaee.entity.emp;/** * Unit Test * @author Indus * */public class Testempdao {//individually tested mybatis@testpublic void test1 () th Rows Exception{empdao Empdao = new Empdao () Empdao.add (New Emp (1, "haha", 7000D, "male")); Test Spring integration Mybatis@testpublic void Test2 () throws Exception{classpathxmlapplicationcontext AC = new Classpathxmlapplicationcontext ("Spring.xml"); Empdao Empdao = (Empdao) ac.getbean ("Empdao"), Empdao.add (New Emp (3, "haha", 7000D, "male"));}}
Integrated configuration of Spring and MyBatis