首先需要的jar包
mybatis-3.4.1
配置文件(mybatis.cfg.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>
<typeAliases>
<typeAlias alias="mybatis1" type="entity.mybatis1"/>
<typeAlias alias="types" type="entity.types"/>
</typeAliases>
<!-- 选择默认的连接方式 -->
<environments default="myb">
<environment id="myb">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="accp"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="entity/mybatis1Mapper.xml"/>
</mappers>
</configuration>
Mybatis1.java
package entity;
public class mybatis1 {
private int id;
private String name;
private int age;
private types types;
public types getTypes() {
return types;
}
public void setTypes(types types) {
this.types = types;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public mybatis1(String name, int age) {
super();
this.name = name;
this.age = age;
}
public mybatis1(){}
}
Types.java
package entity;
import java.util.ArrayList;
import java.util.List;
public class types {
private int id;
private String tname;
private List<mybatis1> mybatis=new ArrayList<mybatis1>();
public List<mybatis1> getMybatis(){
return mybatis;
}
public void setMybatis(List<mybatis1> mybatis) {
this.mybatis = mybatis;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
}
mybatis1Mapper.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="entity.mybatis1">
<resultMap type="mybatis1" id="mylist">
<id property="id" column="id"></id>
<result column="name" property="name"/>
<result column="age" property="age"/>
<!-- 多对一的关系 -->
<!-- property: 指的是属性的值, javaType:指的是属性的类型-->
<association property="types" javaType="types">
<id column="id" property="id"/>
<result column="tname" property="tname"/>
</association>
</resultMap>
<select id="selectall" resultMap="mylist">
select m.*,t.tname tname from mybatis1 m ,types t
where m.typeid=t.id
</select>
<insert id="addmybatis" parameterType="entity.mybatis1">
insert into mybatis1(name,age) values(#{name},#{age})
</insert>
</mapper>
typesMapper.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="entity.types">
<resultMap type="types" id="mytype">
<id column="id" property="id"></id>
<result column="tname" property="tname"/>
<!-- 一对多的关系 -->
<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<collection property="mybatis" ofType="mybatis1">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
</collection>
</resultMap>
<select id="selectalltype" resultMap="mytype">
select m.*,t.tname tname from mybatis1 m ,types t where m.typeid=t.id
</select>
</mapper>
test_mybatis.java(测试)
package test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
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 entity.mybatis1;
public class test_mybatis {
public static void main(String[] args) {
try {
Reader r= Resources.getResourceAsReader("mybatis.cfg.xml");
SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();
SqlSessionFactory sf=sfb.build(r);
SqlSession session=sf.openSession();
// mybatis1 my=new mybatis1("zs", 12);
// session.insert("entity.mybatis1.addmybatis", my);
// session.commit();
String d="d";
List<mybatis1> list=session.selectList("selectall");
System.out.println(list.size());
for(mybatis1 m:list){
System.out.println(m.getTypes().getTname()+"=="+m.getName());
}
session.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}