Mybatis实例:

对两个表Student、StudentInfo的CRUD操作

在Src目录下建一个mybatis的xml配置文件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>
	<!-- 引入外部 -->
	<properties resource="db.properties"></properties>
	<!-- 定义别名 -->
	<typeAliases>
		<!-- 自动扫描 -->
		<package name="com.model"/>
	</typeAliases>
	
	
	
	<environments default="development">
		<environment id="development">
			<!-- 事务管理器 -->
			<transactionManager type="JDBC"></transactionManager>
			
			<!-- 数据源 -->
			<dataSource type="POOLED">
				<property name="username" value="${username}"/>
				<property name="password" value="${password}"/>
				<property name="url" value="${url}"/>
				<property name="driver" value="${driver}"/>
			
			</dataSource>
		</environment>
	</environments>
	<!-- 扫描包下的接口 -->
	<mappers>
		<package name="com.dao"/>
	</mappers>

</configuration>

定义Student mappers、StudentInfo mappers的StudentMapper.xml、StudentInfoMapper.xml配置文件和StudentMapper、StudentInfoMapper接口

StudentMapper接口

package com.dao;

import java.util.List;
import java.util.Map;

import com.model.Student;

public interface StudentMapper {
	public Integer add(Student stu);
	public void del(String sno);
	public void update(Student stu);
	public Student show(String sno);
	public List<Student> showMany(Map<String,Object> map);
}

StudentInfoMapper接口

package com.dao;

import java.util.List;

import com.model.Studentinfo;

public interface StudentInfoMapper {
	public List<Studentinfo> sel();
}

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="com.dao.StudentMapper">
	<resultMap type="student" id="stulist"></resultMap>
	<insert id="add" parameterType="student">
		insert into student values(#{sno},#{sname},#{ssex},sysdate,#{sclass})
	</insert>
	<delete id="del" parameterType="String">
		delete student s where s.sno=#{sno}
	</delete>
	<update id="update" parameterType="student">
		update student t set t.sname=#{sname},t.ssex=#{ssex}
		,t.sbirthday=sysdate,t.sclass=#{sclass} where t.sno=#{sno}
	</update>
	<select id="show" resultType="student" parameterType="String">
		select * from student s where s.sno=#{sno}
	</select>
	<select id="showMany" resultMap="stulist" parameterType="Map">
		select * from student s where s.sname like #{p1} and s.ssex=#{p2}
	</select>
</mapper>

StudentInfoMapper.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="com.dao.StudentInfoMapper">
	<!-- 查询两个表的关联 方法一 
		<resultMap type="studentinfo" id="stuinfo">
			<id property="id" column="id"/>
			
			<result property="student.sno" column="sno"/>
			<result property="student.sname" column="sname"/>
			<result property="student.ssex" column="ssex"/>
			<result property="student.sbirthday" column="sbirthday"/>
			<result property="student.sclass" column="sclass"/>
			
			<result property="saddress" column="saddress"/>
		</resultMap>
	-->
	<!-- 查询两个表的关联 方法二 -->
	<resultMap type="studentinfo" id="stuinfo">
	
		<association property="student" column="sno" select="com.dao.StudentMapper.show"></association>
	
	</resultMap>
	
	<select id="sel" resultMap="stuinfo">
		select * from studentinfo si left join student s on si.sno=s.sno
	</select>
	
</mapper>

配置文件实现了接口和SQL语句的映射关系。

Student类和StudentInfo类的实体类:

Student类:

package com.model;

import java.util.Date;

public class Student {
	private String sno;
	private String sname;
	private String ssex;
	private Date sbirthday;
	private String sclass;
	public Student() {
		super();
	}
	
	public Student(String sno, String sname, String ssex, Date sbirthday, String sclass) {
		super();
		this.sno = sno;
		this.sname = sname;
		this.ssex = ssex;
		this.sbirthday = sbirthday;
		this.sclass = sclass;
	}
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getSsex() {
		return ssex;
	}
	public void setSsex(String ssex) {
		this.ssex = ssex;
	}
	public Date getSbirthday() {
		return sbirthday;
	}
	public void setSbirthday(Date sbirthday) {
		this.sbirthday = sbirthday;
	}
	public String getSclass() {
		return sclass;
	}
	public void setSclass(String sclass) {
		this.sclass = sclass;
	}
	
	@Override
	public String toString() {
		return "Student [sno=" + sno + ", sname=" + sname + ", ssex=" + ssex + ", sbirthday=" + sbirthday + ", sclass="
				+ sclass + "]";
	}
}

StudentInfo类:

package com.model;

public class Studentinfo {
	private Integer id;
	private Student student;
	private String saddress;
	public Studentinfo() {
		super();
	}
	public Studentinfo(Integer id, Student student, String saddress) {
		super();
		this.id = id;
		this.student = student;
		this.saddress = saddress;
	}
	@Override
	public String toString() {
		return "Studentinfo [id=" + id + ", student=" + student + ", saddress=" + saddress + "]";
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public String getSaddress() {
		return saddress;
	}
	public void setSaddress(String saddress) {
		this.saddress = saddress;
	}
}

为了方便写一个工具类SqlSessionUtil 拿出session:

package com.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
 * Mybatis工具类
 * @return
 */
public class SqlSessionUtil {
	private static SqlSessionFactory ssf;
	private static SqlSession ss;
	/**
	 * 获取Mybatis核心SQLsessionFactory
	 * @return
	 */
	public static SqlSessionFactory getSQLSessionFactory(){
		InputStream in = null;
		
		try {
			in = Resources.getResourceAsStream("mybatis-config.xml");
			ssf = new SqlSessionFactoryBuilder().build(in);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	
		return ssf;
	}
	/**
	 * 获取SQLsession
	 * @return
	 */
	public static SqlSession getSQLSession(){
		ss = getSQLSessionFactory().openSession();
		return ss;
	}
	
} 

Junit测试类:

package com.test;

import static org.junit.Assert.*;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.dao.StudentInfoMapper;
import com.model.Studentinfo;
import com.util.SqlSessionUtil;

public class StudentInfoJunit {
	
	private SqlSession ss;
	private StudentInfoMapper sm;
	
	@Before
	public void setUp() throws Exception {
		ss= SqlSessionUtil.getSQLSession();
		sm = ss.getMapper(StudentInfoMapper.class);
	}

	@After
	public void tearDown() throws Exception {
		ss.commit();
		ss.close();
	}

	@Test
	public void test() {
		List<Studentinfo> list = sm.sel();
		for(Studentinfo sinfo : list){
			System.out.println(sinfo);
		}
	}

}

  运行结果如下(实现的两表一对一):

posted on 2017-04-20 16:31  云破月丶  阅读(1299)  评论(0编辑  收藏  举报