util包
package util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MybatisUtil {
private static SqlSessionFactory sessionFactory;
static {
String resource="MyBatis.xml";
try {
InputStream is= Resources.getResourceAsStream(resource);
sessionFactory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSession(){
return sessionFactory.openSession();
}
public static void closeSession(SqlSession session){
if (session!=null){
session.close();
}
}
}
MyBatis.xml 注意该xml文件需要放在src下面里面的实体类包路径和dao包路径需要,以及数据库名称和密码需要改成你自己的
<?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>
<typeAliases>
<package name="pojo"/>
</typeAliases>
<environments default="cs">
<environment id="cs">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/fx01"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/FxxDao.xml"></mapper>
</mappers>
</configuration>
pojo包的两个实体类
package pojo;
public class Fxx {
private int id;
private String name;
private String pwd;
private int clasId;
private Clas clas;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getClasId() {
return clasId;
}
public void setClasId(int clasId) {
this.clasId = clasId;
}
public Clas getClas() {
return clas;
}
public void setClas(Clas clas) {
this.clas = clas;
}
public Fxx(int id, String name, String pwd, int clasId, Clas clas) {
this.id = id;
this.name = name;
this.pwd = pwd;
this.clasId = clasId;
this.clas = clas;
}
@Override
public String toString() {
return "Fxx{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
", clasId=" + clasId +
", clas=" + clas +
'}';
}
public Fxx() {
}
public Fxx(int id, String name, String pwd, int clasId) {
this.id = id;
this.name = name;
this.pwd = pwd;
this.clasId = clasId;
}
}
package pojo;
import java.util.List;
public class Clas {
private int clasId;
private String clasName;
private List<Fxx> fxxList;
public int getClasId() {
return clasId;
}
public void setClasId(int clasId) {
this.clasId = clasId;
}
public String getClasName() {
return clasName;
}
public void setClasName(String clasName) {
this.clasName = clasName;
}
public List<Fxx> getFxxList() {
return fxxList;
}
public void setFxxList(List<Fxx> fxxList) {
this.fxxList = fxxList;
}
public Clas() {
}
@Override
public String toString() {
return "Clas{" +
"clasId=" + clasId +
", clasName='" + clasName + '\'' +
", fxxList=" + fxxList +
'}';
}
public Clas(String clasName, List<Fxx> fxxList) {
this.clasName = clasName;
this.fxxList = fxxList;
}
public Clas(int clasId, String clasName, List<Fxx> fxxList) {
this.clasId = clasId;
this.clasName = clasName;
this.fxxList = fxxList;
}
public Clas(String clasName) {
this.clasName = clasName;
}
public Clas(List<Fxx> fxxList) {
this.fxxList = fxxList;
}
}
dao包下的接口
package dao;
import pojo.Clas;
import pojo.Fxx;
import java.util.List;
public interface FxxDao {
//多对一
public List<Fxx> findAll();
//一对多
public Clas findByClasId(int clasId);
}
dao包下的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="dao.FxxDao">
<resultMap id="fxx" type="Fxx">
<result property="id" column="id"></result>
<result property="name" column="name"></result>
<result property="pwd" column="pwd"></result>
<result property="clasId" column="clasId"></result>
<association property="clas" javaType="Clas" resultMap="clases"></association>
</resultMap>
<resultMap id="clases" type="Clas">
<result property="clasId" column="clasId"></result>
<result property="clasName" column="clasName"></result>
</resultMap>
<resultMap id="claslist" type="Clas">
<result property="clasId" column="clasId"></result>
<result property="clasName" column="clasName"></result>
<collection property="fxxList" ofType="Fxx">
<result property="id" column="id"></result>
<result property="name" column="name"></result>
<result property="pwd" column="pwd"></result>
<result property="clasId" column="clasId"></result>
</collection>
</resultMap>
<select id="findAll" resultMap="fxx">
select f.*,c.clasName from fxx f, clas c where f.clasId=c.clasId
</select>
<select id="findByClasId" resultMap="claslist">
select f.*,c.clasName from fxx f, clas c where f.clasId=c.clasId and f.clasId=#{clasId}
</select>
</mapper>
test包下的测试类
package test;
import dao.FxxDao;
import org.apache.ibatis.session.SqlSession;
import pojo.Clas;
import pojo.Fxx;
import util.MybatisUtil;
import java.util.List;
public class Test {
public static void main(String[] args) {
test1();
test2();
}
private static void test2() {
SqlSession session = MybatisUtil.getSession();
Clas ci = session.getMapper(FxxDao.class).findByClasId(1);
System.out.println(ci.getClasName());
for(Fxx f:ci.getFxxList()){
System.out.println(f.getName());
}
}
private static void test1() {
SqlSession session = MybatisUtil.getSession();
List<Fxx> fxxList = session.getMapper(FxxDao.class).findAll();
for (Fxx f:fxxList){
System.out.println(f.getName()+f.getClas().getClasId());
}
}
}