您的位置:首页 > 数据库 > Oracle

spring-mybaits-oracle项目初接触

2017-03-06 16:10 246 查看
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;
  }
  (get,set方法略)
}
5)创建EmpMapper.xml映射文件

<resultMap
type="cn.itcast.javaee.mybatis.entity.Emp"id="empMap">
    
4000
  <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
class
EmpDao {
    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.
caff
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"/>
<!—上面表示凡是以add,delete,update开头的一定要有事务REQUIRED(required)代表一定要事务
-->
<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,"男"));
  }
}

执行后查看数据库,可以看到明明的信息已经被导入到数据库中!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐