1)創建一個sPRing-mybaits-Oracle這么一個javaweb或java工程
2)導入spring,mybatis,c3p0,oracle和 “mybatis提供的與spring整合的插件包”
3)創建emps.sql表,使用oracle或MySQL語法
4)創建Emp.java類
5)創建EmpMapper.xml映射文件
6)創建mybatis.xml配置文件
7)創建EmpDao.java類
8)創建Spring.xml文件
9)找到測試類,右擊junit,運行代碼
具體如下:
3)
--oracle語法
create table emps(
eid number(5) primarykey,
ename varchar2(20),
esal number(8,2),
esex varchar2(2)
);
--mysql語法
create table emps(
eid int(5) primarykey,
ename varchar(20),
esal int(8),
esex varchar(2)
);
4)創建Emp.java類
packagecn.itcast.javaee.mybatis.entity;
/**
* 員工
* @author AdminTC
*/
public class Emp {
private Integerid;
private Stringname;
private Doublesal;
private Stringsex;
public Emp(){}
public Emp(Integer id, String name, Doublesal, String sex) {
this.id = id;
this.name = name;
this.sal = sal;
this.sex = sex;
}
(get,set方法略)
}
5)創建EmpMapper.xml映射文件
<resultMaptype="cn.itcast.javaee.mybatis.entity.Emp"id="empMap">
<idproperty="id"column="eid"/>
<resultproperty="name"column="ename"/>
<resultproperty="sal"column="esal"/>
<resultproperty="sex"column="esex"/>
</resultMap>
<!-- 增加員工 -->
<insertid="add"parameterType="cn.itcast.javaee.mybatis.entity.Emp">
insert into emps(eid,ename,esal,esex)values(#{id},#{name},#{sal},#{sex})
</insert>
6)創建mybatis.xml配置文件
<configuration>
<environmentsdefault="mysql_developer">
<environmentid="mysql_developer">
<transactionManagertype="jdbc"/>
<dataSourcetype="pooled">
<propertyname="driver"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="****"/>
<propertyname="username"value="****"/>
<propertyname="passWord"value="****"/>
</dataSource>
</environment>
<environmentid="oracle_developer">
<transactionManagertype="jdbc"/>
<dataSourcetype="pooled">
<propertyname="driver"value="oracle.jdbc.driver.OracleDriver"/>
<propertyname="url"value="****"/>
<propertyname="username"value="****"/>
<propertyname="password"value="****"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapperresource="cn/itcast/javaee/mybatis/entity/EmpMapper.xml"/>
</mappers>
</configuration>
7)創建EmpDao.java類
/**
* 持久層
* 實現類
* @author AdminTC
*/
public classEmpDao {
private SqlsessionFactory sqlSessionFactory;
public void setSqlSessionFactory(SqlSessionFactorysqlSessionFactory) {
this.sqlSessionFactory= sqlSessionFactory;
}
/**
* 增加員工
*/
public void add(Emp emp) throws Exception{
SqlSessionsqlSession = sqlSessionFactory.openSession();
sqlSession.insert("empNamespace.add",emp);
sqlSession.close();
}
}
8)創建Spring.xml文件
8.1<!--配置頭文件-->
8.2 <!-- 配置C3P0連接池,目的:管理數據庫連接 -->
8.3<!-- 配置SqlSessionFactoryBean,目的:加載mybaits配置文件和映射文件,即替代原Mybatis工具類的作用 -->
8.4<!-- 配置Mybatis的事務管理器,即因為Mybatis底層用的是JDBC事務管事器,所以在這里依然配置JDBC事務管理器 -->
8.5<!-- 配置事務通知,即讓哪些方法需要事務支持 -->
8.6<!-- 配置事務切面,即讓哪些包下的類需要事務 -->
8.7 <!-- 注冊EmpDao -->
具體:
8.1配置頭文件
<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
">
8.2 <!-- 配置C3P0連接池,目的:管理數據庫連接 -->
<beanid="comboPooledDataSourceID"class="com.mchange.v2.c3p0.ComboPooledDataSource">
<propertyname="driverClass"value="oracle.jdbc.driver.OracleDriver"/>
<propertyname="jdbcUrl"value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<propertyname="user"value="scott"/>
<propertyname="password"value="tiger"/>
</bean>
8.3<!-- 配置SqlSessionFactoryBean,目的:加載mybaits配置文件和映射文件,即替代原Mybatis工具類的作用 -->
<beanid="sqlSessionFactoryBeanID"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="configLocation"value="classpath:mybatis.xml"/>
<propertyname="dataSource"ref="comboPooledDataSourceID"/>
</bean>
8.4<!-- 配置Mybatis的事務管理器,即因為Mybatis底層用的是JDBC事務管事器,所以在這里依然配置JDBC事務管理器 -->
<beanid="dataSourceTransactionManagerID"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="comboPooledDataSourceID"/>
</bean>
8.5<!-- 配置事務通知,即讓哪些方法需要事務支持 -->
<tx:adviceid="tx"transaction-manager="dataSourceTransactionManagerID">
<tx:attributes>
<tx:methodname="*"propagation="REQUIRED"/>
<!—*意味著所有的方法都要事務-->
</tx:attributes>
</tx:advice>
(實際項目中很多可能是以下這種效果)
<tx:attributes>
<tx:methodname="add*"propagation="REQUIRED"/>
<tx:methodname="delete*"propagation="REQUIRED"/>
<tx:methodname="update*"propagation="REQUIRED"/>
<!—上面表示凡是以add,delete,update開頭的一定要有事務REQUIRED(required)代表一定要事務 -->
<tx:methodname=" *"propagation="SUPPOETS"/>
<!—*表示其他方法,方法中有事務就有,沒有事務就沒有-->
</tx:attributes>
8.6<!-- 配置事務切面,即讓哪些包下的類需要事務 -->
<aop:config>
<aop:pointcutid="pointcut"expression="execution(*cn.itcast.javaee.mybatis.dao.*.*(..))"/>
<aop:advisoradvice-ref="tx"pointcut-ref="pointcut"/>
</aop:config>
(id="pointcut"中如果是實際的項目應該是模塊名+ pointcut,如adminPointcut
*代表返回值,返回值不限值,一定要在cn.itcast.javaee.mybatis.dao下
advice-ref="tx"pointcut-ref="pointcut"表示將tx的事務切入到pointcut)
8.7 <!--注冊EmpDao -->
<beanid="empDaoID"class="cn.itcast.javaee.mybatis.dao.EmpDao">
<propertyname="sqlSessionFactory"ref="sqlSessionFactoryBeanID"/>
</bean>
9)找到測試類,右擊junit,運行代碼
/**
* 單元測試
* @author AdminTC
*/
public class TestEmpDao {
//單獨測試mybatis
@Test
public void test1() throws Exception{
EmpDao empDao = new EmpDao();
empDao.add(new Emp(1,"哈哈",7000D,"男"));
}
//測試spring整合mybatis
@Test
public void test2() throws Exception{
applicationContext ac = newClassPathXmlApplicationContext(new String[]{"spring.xml"});
EmpDao empDao = (EmpDao)ac.getBean("empDaoID");
empDao.add(new Emp(1,"明明",8000D,"男"));
}
}
執行后查看數據庫,可以看到明明的信息已經被導入到數據庫中!
新聞熱點
疑難解答