spring + mybatis + mysql/oracle开发

1)创建一个spring-mybatis-mysql这么一个javaweb或java工程

2)导入spring-ioc,spring-aop,spring-transaction,mybatis,c3p0,mysql/oracle相关的jar包和spring整合mybatis的jar包

3)创建students.sql

--mysql
create table students(
   sid  int(5) primary key,
   sname varchar(10),
   ssal double(8,2)
);

4)创建Student.java

/**
 * 学生*/
public class Student {
    private Integer id;//编号
    private String name;//姓名
    private Double sal;//薪水
    public Student(){}
    public Student(Integer id, String name, Double sal) {
        this.id = id;
        this.name = name;
        this.sal = sal;
    }
    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;
    }
}

5)创建StudentMapper.xml

<?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="studentNamespace">
    <resultMap type="loaderman.entity.Student" id="studentMap">
        <id property="id" column="sid" />
        <result property="name" column="sname"/>
        <result property="sal" column="ssal"/>
    </resultMap>
    <insert id="insert" parameterType="loaderman.entity.Student">
        insert into students(sid,sname,ssal) values(#{id},#{name},#{sal})
    </insert>
</mapper>

6)创建StudentDao.java

public class StudentDao {
    private SqlSessionFactory sqlSessionFactory;
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }
    public void insert(Student student){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.insert("studentNamespace.insert",student);
        //int i = 10/0;
    }
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});
        StudentDao studentDao = (StudentDao) ac.getBean("studentDaoID");
        studentDao.insert(new Student(1,"哈哈",7000D));
    }
}

7)在src目录下创建mybatis.xml

<?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>
    <mappers>
        <mapper resource="loaderman/entity/StudentMapper.xml"/>
    </mappers>
</configuration>

8)在src目录下创建spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<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
        
      ">

      <!-- 配置C3P0连接池(即管理数据库连接) -->
      <bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource">
              <property name="driverClass" value="com.mysql.jdbc.Driver"/>
              <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/mybatis"/>
              <property name="user" value="root"/>
              <property name="password" value="root"/>
      </bean>
      
      
      <!-- 配置SqlSessionFactoryBean(即替代MyBatisUtil工具类的作用) -->
      <bean id="sqlSessionFactoryBeanID" class="org.mybatis.spring.SqlSessionFactoryBean">
              <property name="dataSource" ref="comboPooledDataSourceID"/>
              <property name="configLocation" value="classpath:mybatis.xml"/>
      </bean>
      
      
      <!-- 配置事务管理器(即使用JDBC事务管理器) -->
      <bean id="dataSourceTransactionManagerID" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
              <property name="dataSource" ref="comboPooledDataSourceID"/>
      </bean>
      
      
      <!-- 配置事务通知(即哪些方法需要事务) -->
      <tx:advice id="tx" transaction-manager="dataSourceTransactionManagerID">
              <tx:attributes>
                  <tx:method name="*" propagation="REQUIRED"/>
              </tx:attributes>
      </tx:advice>     

      
      <!-- 配置事务切面(即哪些包中的类需要事务通知) -->
      <aop:config>
              <aop:pointcut id="pointcut" expression="execution(* loaderman.dao.*.*(..))"/>
              <aop:advisor advice-ref="tx" pointcut-ref="pointcut" />
      </aop:config>
      
      
      <!-- 配置StudentDao类 -->
      <bean id="studentDaoID" class="loaderman.dao.StudentDao">
              <property name="sqlSessionFactory" ref="sqlSessionFactoryBeanID"/>
      </bean>
      
</beans>

 

posted on 2018-12-04 16:02  LoaderMan  阅读(381)  评论(0编辑  收藏  举报

导航