spring3+mbatis3开发实例

阅读更多
最近一直在深入了解struts2,spring,hibernate以及mybatis框架,通过查看这些框架的源码和官方文档,发现自己对于这些框架的原理,使用有了更深的理解,那么今天
我给大家带来的是运用spring和mybatis这两个框架来开发的小例子,并给大家讲述一些开发中需要注意的一些细节。
1、新建一个web项目,修改web.xml文件,我的文件内容如下,大家把需要的拷走就行:


	dreamMall-dubbo-provider
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		org.springframework.web.util.Log4jConfigListener
	
	
	
		org.springframework.web.util.IntrospectorCleanupListener
	
	
	
		contextConfigLocation
		classpath:spring-*.xml
	
	
	
		log4jConfigLocation
		classpath:log4j.properties
	
	
	
		log4jRefreshInterval
		60000
	
	
	
		CharacterEncoding
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
		
			forceEncoding
			true
		
	
	
		CharacterEncoding
		/*
	
	
	
         30
    

2、新建spring-app.xml和spring-mybatis.xml文件,其中:
spring-app.xml:
  

    
   
 

注释的方法大家不用管,这些是我在下一篇博客要给大家带来的关于spring中aop的技术以及使用aop实现事务的管理

spring-mybatis.xml:


	
		
			classpath:jdbc.properties
		
	
	
	
	
		
		
		
		
	


	
		
		
	

	
	
		
		
		 
	


 


这里大家需要关注的是Mapper的注册方式,这里我采用的所有的dao都继承GenericiDao,这样的话,你就不需要再去一个一个Mapper的注册,开发中经常使用的是这种方式。

3、这里我需要简单的表述一下我的数据库中表的对于关系,我用了测试的有三个表,teacher表,cource表,student表,我假定一个teacher教一门Cource,一个teacher交
n个student,下面我用3个实体类来描述着三者的关系:
cource.java:
package com.mall.dubbo.entity;

import java.io.Serializable;

public class Cource implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int id;
	
	private String courceName;
	
	private int teacherId;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	

	public int getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(int teacherId) {
		this.teacherId = teacherId;
	}

	
	public String getCourceName() {
		return courceName;
	}

	public void setCourceName(String courceName) {
		this.courceName = courceName;
	}

	@Override
	public String toString() {
		return "Cource [id=" + id + ", courceName=" + courceName + "]";
	}
	
	

}

student.java:
package com.mall.dubbo.entity;

import java.io.Serializable;

public class Student implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int id;
	
	private int age;
	
	private String studentName;
	
	private int sex;
	
	private int teacherId;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getStudentName() {
		return studentName;
	}

	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}

	public int getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(int teacherId) {
		this.teacherId = teacherId;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", age=" + age + ", studentName="
				+ studentName + ", sex=" + sex + "]";
	}
	
	

}

Teacher.java:
package com.mall.dubbo.entity;

import java.io.Serializable;
import java.util.List;

public class Teacher implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int id;
	
	private String teacherName;
	
	private int sex;
	
	private int age;
	
	private Cource cource;
	
	private List students;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	

	public Cource getCource() {
		return cource;
	}

	public void setCource(Cource cource) {
		this.cource = cource;
	}

	public List getStudents() {
		return students;
	}

	public void setStudents(List students) {
		this.students = students;
	}

	@Override
	public String toString() {
		return "Teacher [id=" + id + ", teacherName=" + teacherName + ", sex="
				+ sex + ", age=" + age + "]";
	}
	
}

4、建立映射关系,这部分是最重要的,这里面的知识点,我希望大家可以好好看,如果有什么问题的,可以评论留言,或者去查看mybatis3.2的官方文档
地址是 http://wenku.baidu.com/link?url=L6Lu0GufwrMCgBLGUbsfGy7Os6s7MEcKIsZQj7JhOxIo6BSbsULynqsWeqX0mIyIqkzLIozaQvnaAUROrWypUDQj3QBfe5j6jO3solfO3-G
cource.xml:



	
		
		
		
	
	
	
		
		
		
	
	
	id,cource_name,teacher_id
	
	
	
	
	
	
		insert into cource () values(
			#{id,javaType=int,jdbcType=INTEGER},
			#{courceName,javaType=String,jdbcType=VARCHAR},
			#{teacherId,javaType=int,jdbcType=INTEGER}
		)
	
	
	
		update cource set
			cource_name=#{courceName,javaType=String,jdbcType=VARCHAR},
			teacher_id=#{teacherId,javaType=int,jdbcType=INTEGER}
		where id=#{id,javaType=int,jdbcType=INTEGER}
	
	
	
		delete from cource where id=#{id,javaType=int,jdbcType=INTEGER}
	
	
		delete from cource
	


student.xml:



	
		
		
		
		
		
	
	
		
		
		
		
		
	
	id,age,student_name,sex,teacher_id
	
	
	
	
	
	
		insert into student() values (
			#{id,javaType=int,jdbcType=INTEGER},
			#{age,javaType=int,jdbcType=INTEGER},
			#{studentName,javaType=String,jdbcType=VARCHAR},
			#{sex,javaType=int,jdbcType=INTEGER},
			#{teacherId,javaType=int,jdbcType=INTEGER}
		)
	
	
	
		update student set
			age=#{age,javaType=int,jdbcType=INTEGER},
			student_name=#{studentName,javaType=String,jdbcType=VARCHAR},
			sex=#{sex,javaType=int,jdbcType=INTEGER},
			teacher_id=#{teacherId,javaType=int,jdbcType=INTEGER}
		where id=#{id,javaType=int,jdbcType=INTEGER}
	
	
	
		delete from student where id=#{id,javaType=int,jdbcType=INTEGER}
	
	
	
		delete from student
	


teacher.xml:




	
		
		
		
		
		
		
		
			
			
			
		
		
		
		
		

		
		
		
			
			
			
			
			
		
		
		
		
		
	

	
	
	
	
	
		
		
		
		
	
	id,teacher_name,age,sex
	
	
	
	
	
	
		insert into teacher(
		
		)values(
		#{id,javaType=int,jdbcType=INTEGER},
		#{teacherName,javaType=String,jdbcType=VARCHAR},
		#{age,javaType=int,jdbcType=INTEGER},
		#{sex,javaType=int,jdbcType=INTEGER}
		)
	


以上就是三者的映射关系,在这里对于student.xml和cource.xml我不想过多的去说,大家注意resultMap,resultType,paramType,paramMap这四个属性的不同就行
我重点讲一下teacher.xml文件中的映射关系,这里面涉及到collection和association这两个元素,这两个元素分别代表者1对多和1对1的关系,需要注意的是,
查询的结果总不要有相同的字段名,如果存在相同的字段名会覆盖,从而导致查询的结果不对。

5、对应的Dao接口
GenericDao:
package com.mall.dubbo.dao;

public interface GenericDao {

}

该接口是所有的dao接口的父接口,对于与配置文件中的markerInterface

CourceDao:
package com.mall.dubbo.dao;

import org.springframework.stereotype.Repository;

import com.mall.dubbo.entity.Cource;

@Repository
public interface CourceDao extends GenericDao {

	public abstract void addCource(Cource cource);
	
	public abstract Cource selectById(int id);
	
	public abstract void updateCourceById(Cource cource);
}


StudentDao:
package com.mall.dubbo.dao;

import com.mall.dubbo.entity.Student;
@Repository
public interface StudentDao extends GenericDao {

	public abstract void addStudent(Student student);
}


TeacherDao:

package com.mall.dubbo.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.mall.dubbo.entity.Teacher;

@Repository
public interface TeacherDao extends GenericDao{

	public abstract void addTeacher(Teacher teacher);
	
	public abstract Teacher selectTeacerById(int id);
	
	public abstract List selectAll();
	
}


6、编写测试类
CourceTest.java:
package com.mall.dubbo.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.mall.dubbo.dao.CourceDao;
import com.mall.dubbo.entity.Cource;

public class CourceTest {

	private ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring-mybatis.xml");
	
	@Test
	public void addCource(){
		
		Cource cource = new Cource();
		cource.setCourceName("English");
		cource.setId(2);
		cource.setTeacherId(1);
		
		CourceDao dao = context.getBean(CourceDao.class);
		
		dao.addCource(cource);
	}
	
	//@Test
	public void getCourceById(){
		
		CourceDao dao = context.getBean(CourceDao.class);
		
		Cource c = dao.selectById(1);
		
		System.out.println(c);
	}
	
	//@Test
	public void updateCourceById(){
		Cource cource = new Cource();
		cource.setCourceName("France");
		cource.setId(1);
		cource.setTeacherId(2);
		CourceDao dao = context.getBean(CourceDao.class);
		dao.updateCourceById(cource);
	}
}


StudentTest.java:
package com.mall.dubbo.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.mall.dubbo.dao.StudentDao;
import com.mall.dubbo.entity.Student;

public class StudentTest {

	ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring-mybatis.xml");
	
	StudentDao dao = context.getBean(StudentDao.class);
	
	@Test
	public void addStudentTest(){
		
		Student stu = new Student();
		stu.setAge(21);
		stu.setId(3);
		stu.setSex(1);
		stu.setStudentName("zhangqi");
		stu.setTeacherId(1);
		
		dao.addStudent(stu);
	}
}


TeacherTest.java:
package com.mall.dubbo.test;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.mall.dubbo.dao.TeacherDao;
import com.mall.dubbo.entity.Cource;
import com.mall.dubbo.entity.Student;
import com.mall.dubbo.entity.Teacher;

public class TeacherTest {

	ApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring-mybatis.xml");

	TeacherDao dao = context.getBean(TeacherDao.class);
	
	//@Test
	public void addTeacherTest(){
		
		Teacher t = new Teacher();
		
		t.setAge(45);
		t.setId(1);
		t.setSex(0);
		t.setTeacherName("liusanming");
		
		dao.addTeacher(t);
	}
	
	@Test
	public void getTeacherById(){
		
		Teacher t = dao.selectTeacerById(1);
		Cource c = t.getCource();
		List stus = t.getStudents();
		System.out.println(stus+","+stus.size());
		System.out.println(c);
		System.out.println(t);
	}
	//@Test
	public void getTeachers(){
		List ts = dao.selectAll();
		System.out.println(ts.size());
		Teacher t = ts.get(0);
		Cource c = t.getCource();
		List stus = t.getStudents();
		System.out.println(stus+","+stus.size());
		System.out.println(c);
		System.out.println(t);

	}
}

到此整个的开发过程就结束了,内容有点多,希望能够对大家有帮助。大家如果有什么问题,请留言!
本人也毕业未满一年,如果讲的有什么不对的地方,请指正,我们一块进步,谢谢!

你可能感兴趣的:(spring,mybatis,dao,框架,mvc)