spring-mybaits-oracle项目初接触

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 Integer id;

  private String name;

  private Double sal;

  private String sex;

  public Emp(){}

  public Emp(Integer id, String name, Doublesal, String sex) {

      this.id = id;

      this.name = name;

      this.sal = sal;

      this.sex = sex;

  }

  getset方法略)

}

5)创建EmpMapper.xml映射文件

<resultMap type="cn.itcast.javaee.mybatis.entity.Emp"id="empMap">

      <id property="id"column="eid"/>

      <result property="name"column="ename"/>

      <result property="sal"column="esal"/>

      <result property="sex"column="esex"/>

  </resultMap>   

 

  <!-- 增加员工 -->

  <insert id="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>

  <environments default="mysql_developer">

      <environment id="mysql_developer">

          <transactionManager type="jdbc"/>  

          <dataSource type="pooled">

              <property name="driver" value="com.mysql.jdbc.Driver"/>

              <property name="url" value="****"/>

              <property name="username" value="****"/>

              <property name="password" value="****"/>

          </dataSource>

      </environment>

      <environment id="oracle_developer">

          <transactionManager type="jdbc"/>  

          <dataSource type="pooled">

              <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>

              <property name="url" value="****"/>

              <property name="username" value="****"/>

              <property name="password" value="****"/>

          </dataSource>

      </environment>

  </environments>

 

  <mappers>

      <mapper resource="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连接池,目的:管理数据库连接 -->

     <bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource">

            <property name="driverClass"value="oracle.jdbc.driver.OracleDriver"/>

            <property name="jdbcUrl"value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>

            <property name="user"value="scott"/>

            <property name="password"value="tiger"/>

      </bean>

 

8.3<!-- 配置SqlSessionFactoryBean,目的:加载mybaits配置文件和映射文件,即替代原Mybatis工具类的作用 -->

     <bean id="sqlSessionFactoryBeanID" class="org.mybatis.spring.SqlSessionFactoryBean">

            <property name="configLocation"value="classpath:mybatis.xml"/>

            <property name="dataSource"ref="comboPooledDataSourceID"/>

      </bean>

8.4<!-- 配置Mybatis的事务管理器,即因为Mybatis底层用的是JDBC事务管事器,所以在这里依然配置JDBC事务管理器 -->

     <bean id="dataSourceTransactionManagerID" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

            <property name="dataSource"ref="comboPooledDataSourceID"/>

      </bean>

8.5<!-- 配置事务通知,即让哪些方法需要事务支持 -->

     <tx:advice id="tx" transaction-manager="dataSourceTransactionManagerID">

            <tx:attributes>

                <tx:method name="*"propagation="REQUIRED"/>

                    <!—*意味着所有的方法都要事务-->

 

            </tx:attributes>

      </tx:advice>

(实际项目中很多可能是以下这种效果)

<tx:attributes>

            <tx:method name="add*"propagation="REQUIRED"/>

<tx:method name="delete*" propagation="REQUIRED"/>

<tx:method name="update*" propagation="REQUIRED"/>

<!—上面表示凡是以adddeleteupdate开头的一定要有事务REQUIREDrequired)代表一定要事务 -->

<tx:method name=" *" propagation="SUPPOETS"/>

        <!—*表示其他方法,方法中有事务就有,没有事务就没有-->

   </tx:attributes>

 8.6<!-- 配置事务切面,即让哪些包下的类需要事务 -->

     <aop:config>

            <aop:pointcut id="pointcut"expression="execution(*cn.itcast.javaee.mybatis.dao.*.*(..))"/>

            <aop:advisor advice-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 -->

    <bean id="empDaoID" class="cn.itcast.javaee.mybatis.dao.EmpDao">

            <property name="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 = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});

      EmpDao empDao = (EmpDao)ac.getBean("empDaoID");

      empDao.add(new Emp(1,"明明",8000D,""));

  }

}


执行后查看数据库,可以看到明明的信息已经被导入到数据库中!






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值