MyBatis04(测试连接数据库crud 两张表联查)

目录结构:

1.配置mybatis和jdbc同MyBatis02一样https://blog.csdn.net/qq_33371766/article/details/80381907

2.创建新的Address类:

public class Address {
private Integer id;
private String sheng;
private String shi;
private String qu;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSheng() {
return sheng;
}
public void setSheng(String sheng) {
this.sheng = sheng;
}
public String getShi() {
return shi;
}
public void setShi(String shi) {
this.shi = shi;
}
public String getQu() {
return qu;
}
public void setQu(String qu) {
this.qu = qu;
}
@Override
public String toString() {
return "Address [id=" + id + ", sheng=" + sheng + ", shi=" + shi
+ ", qu=" + qu + "]";
}

}

 

Student类:

public class Student {
private Integer id;
private String name;
private Integer age;
private Address address;

public Student() {
super();
}

public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Student(String name, Integer age) {
super();
this.name = name;
this.age = 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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", address=" + address + "]";
}
}

3.mappers:

import com.model.Address;
public interface AddressMapper {
public Address findById(Integer id);
}

 

AddressMapper.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.mappers.AddressMapper">
<resultMap type="Address" id="AddressResult">
<result property="id" column="id"/>
<result property="sheng" column="sheng"/>
<result property="shi" column="shi"/>
<result property="qu" column="qu"/>
</resultMap>

<select id="findById" parameterType="Integer" resultType="Address">
select * from t_address where id=#{id}
</select>
</mapper> 

 

StudentMapper:

import java.util.List;
import com.model.Student;
public interface StudentMapper {
public int add(Student student);

public int update(Student student);

public int delete(Integer id);

public Student findById(Integer id);

public List<Student> find();

public Student findStudentWithAddress(Integer id);
}

 

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.mappers.StudentMapper">


<!-- <resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>

<result property="address.id" column="addressId"/>
<result property="address.sheng" column="sheng"/>
<result property="address.shi" column="shi"/>
<result property="address.qu" column="qu"/>
</resultMap> -->

<!-- <resultMap type="Address" id="AddressResult">
<result property="id" column="id"/>
<result property="sheng" column="sheng"/>
<result property="shi" column="shi"/>
<result property="qu" column="qu"/>
</resultMap>

<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" resultMap="AddressResult"/>
</resultMap> -->

<!-- <resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" javaType="Address">
<result property="id" column="id"/>
<result property="sheng" column="sheng"/>
<result property="shi" column="shi"/>
<result property="qu" column="qu"/>
</association>
</resultMap> -->

<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" column="id" select="com.mappers.AddressMapper.findById"></association>
</resultMap>

<select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
</select>


<insert id="add" parameterType="Student"  >
insert into t_student values(null,#{name},#{age})
</insert>


<update id="update" parameterType="Student">
update t_student set name=#{name},age=#{age} where id=#{id}
</update>

<delete id="delete" parameterType="Integer">
delete from t_student where id=#{id}
</delete>

<select id="findById" parameterType="Integer" resultType="Student">
select * from t_student where id=#{id}
</select>

<select id="find" resultMap="StudentResult">
select * from t_student
</select>
</mapper> 

 

4.测试类:StudentTest:

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


import com.mappers.StudentMapper;
import com.model.Student;
import com.util.SqlSessionFactoryUtil;

public class StudentTest{
private static Logger logger=Logger.getLogger(StudentTest.class);
private SqlSession sqlSession=null;
private StudentMapper studentMapper=null;

/**
* 测试方法前调用
* @throws Exception
*/
@Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
studentMapper=sqlSession.getMapper(StudentMapper.class);
}


/**
* 测试方法后调用
* @throws Exception
*/
@After
public void tearDown() throws Exception {
sqlSession.close();
}


@Test
public void testFindStudentWithAddress() {
logger.info("查询学生(带地址)");
Student student=studentMapper.findStudentWithAddress(2);
System.out.println(student);
} 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值