SpringはmyBatisを統合し、JDBC事務管理に加入する.

31246 ワード

SpringはmyBatisを統合し、JDBC事務管理に加入する.
私が理解している事務管理とは、一旦方法に異常が発生したら、ロールバックして、彼にデータベースの関連操作をさせないと、安全性を高めることができるということです.
1.jarパッケージを導入し、ssmのすべてのjarバッグを中に直接入れます.
2.自分で作成したデータベースによって、comp.wangningbo.bean.Studentエンティティ類を新規作成します.
package com.wangningbo.bean;

public class Student {

	private Integer id;
	private String name;
	private int age;
	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 int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
}

3.comp.wangningbo.service.IStudentServiceインターフェースを作成します.ここでは便利のために、挿入と検索方法を定義しました.
package com.wangningbo.service;

import java.util.List;
import com.wangningbo.bean.Student;

public interface IStudentService {
	List<Student> queryAllStudent();
    void addStudent(Student student);
}

4.serviceを作成する実現類StudentServiveImpl
package com.wangningbo.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wangningbo.bean.Student;
import com.wangningbo.dao.IStudentDao;

//           bean,id="studentService"
@Service("studentService")
//          
@Transactional
public class StudentServiceImpl implements IStudentService {
    //           ,  name, dao  
	@Resource
	private IStudentDao dao;
	public List<Student> queryAllStudent() {
		
		return dao.selectAllStudent();
	}

	public void addStudent(Student student) {
		dao.insertStudent(student);
		int r=100/0;

	}

}

4.dao層インターフェースを定義し、データに対する操作
package com.wangningbo.dao;

import java.util.List;

import com.wangningbo.bean.Student;

public interface IStudentDao {
	List<Student> selectAllStudent();
    void insertStudent(Student student);
}

5.mapper.mapper.xmlを定義してsql文を定義する


<mapper namespace="com.wangningbo.dao.IStudentDao">
    
    <insert id="insertStudent" parameterType="com.wangningbo.bean.Student">
        insert into student(name,age) values(#{name},#{age})
    insert>
    
    <select id="selectAllStudent" resultType="com.wangningbo.bean.Student">
        select * from student
    select>
mapper>
6.最後にSpringコアファイルを作成します.


<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="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        
	   
	   <context:property-placeholder location="classpath:jdbc.properties"/>
	   
	   
	   <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
	      <property name="driverClassName" value="${jdbc.driver}"/>
	      <property name="url" value="${jdbc.url}"/>
	      <property name="username" value="${jdbc.user}"/>
	      <property name="password" value="${jdbc.password}"/>
	   bean>
	  
	   
	   <bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	      
	      <property name="dataSource" ref="myDataSource"/>
	      
	      <property name="mapperLocations" value="classpath:mapper/*.xml"/>
	   bean>
	   
       
       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
           <property name="basePackage" value="com.wangningbo.dao"/>
       bean>
       
       
       <bean id="transactionManager"       class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
           <property name="dataSource" ref="myDataSource"/>
       bean>
       
       
       <tx:annotation-driven transaction-manager="transactionManager"/>
       
       
       <context:component-scan base-package="com.wangningbo"/>
	
beans>

7.comp.wangningbo.test.testテスト方法を最後に作成し、事務管理に加入していない時、方法は異常を報告しましたが、同じように成功データを保存し、追加した後、データベースのロールバック操作を実現しました.
package com.wangningbo.test;

import java.util.List;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wangningbo.bean.Student;
import com.wangningbo.dao.IStudentDao;

public class test {
	@Test
	public void test() {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		IStudentDao service =(IStudentDao) context.getBean("studentService");
		Student student = new Student("WNB",20);
		service.insertStudent(student);
		List<Student> students = service.selectAllStudent();
		for (Student student1: students) {
			System.out.println(student1);	
		}
	}
}