spring-mybaits-oracle项目初接触

来源:互联网 发布:计算机二级vb题库2017 编辑:程序博客网 时间:2024/06/05 18:49

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;

  }

  getset方法略)

}

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"/>

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

<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,""));

  }

}


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






0 0