一对多开发
这里javaBean就不写代码了,一个是Grades类,一个是学生类
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">
<!-- namespace属性是名称空间 必须唯一s\ -->
<mapper namespace="studentNamespace">
<!-- 当实体属性和表字段名不相同的时候, 必须书写以下代码
-->
<resultMap type="onebymany.Student" id="studentMap">
<id property="id" column="sid"/>
<result property="name" column="sname" />
</resultMap>
<select id="findAllByName" parameterType="string" resultMap="studentMap">
select s.sid,s.sname
from students2 s inner join grades g
where s.sgid = g.gid
and g.gname = #{name}
</select>
</mapper>
GradesMapper,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">
<!-- namespace属性是名称空间 必须唯一s\ -->
<mapper namespace="gradesNamespace">
<!-- 当实体属性和表字段名不相同的时候, 必须书写以下代码
-->
<resultMap type="onebymany.Grades" id="gradesMap">
<id property="id" column="gid"/>
<result property="name" column="gname" />
</resultMap>
<select id="findByName" parameterType="string" resultMap="gradesMap">
select g.gname
from students2 s inner join grades g
on s.sgid = g.gid
and s.sname = #{name}
</select>
</mapper>
测试类:
package onebymany;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import util.MybatisUtil;
/**
* 持久层
* @author user
*
*/
public class GradesStudentDao {
/**
* 查询java班级学科有哪些学生
*@param name 学科的名
*/
public List<Student> findAllByName(String name) {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
return sqlSession.selectList("studentNamespace.findAllByName", name);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
MybatisUtil.closeSqlSession();
}
return null;
}
/***
* 查询某个学生是哪个学科的学生
* @param name 代表学生姓名
* @return
*/
public Grades findByName(String name) {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
return sqlSession.selectOne("gradesNamespace.findByName", name);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
MybatisUtil.closeSqlSession();
}
return null;
}
public static void main(String[] args) {
GradesStudentDao dao = new GradesStudentDao();
// List<Student> list = dao.findAllByName("java");
// System.out.println("java学科有"+list.size()+"个学生 信息如下:");
// for (Student student : list) {
// System.out.println(student.getId()+" "+student.getName());
// }
Grades grades = dao.findByName("张三");
System.out.println("张三是:"+grades.getName()+"学科的");
}
}
多对多开发
Bean实例和数据库按照上图建立即可
重点看一下配置文件的编写
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">
<!-- namespace属性是名称空间 必须唯一s\ -->
<mapper namespace="studentNamespace">
<!-- 当实体属性和表字段名不相同的时候, 必须书写以下代码
-->
<resultMap type="manybymany.Student" id="studentMap">
<id property="id" column="sid"/>
<result property="name" column="sname" />
</resultMap>
<select id="findAllByCourseName" parameterType="string" resultMap="studentMap">
select s.sname
from student3 s inner join middles m
on s.sid = m.msid
inner join coures c
on m.mcid = c.cid
and c.cname = #{name}
</select>
</mapper>
CourseMapper.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">
<!-- namespace属性是名称空间 必须唯一s\ -->
<mapper namespace="courseNamespace">
<!-- 当实体属性和表字段名不相同的时候, 必须书写以下代码
-->
<resultMap type="manybymany.Course" id="courseMap">
<id property="id" column="cid"/>
<result property="name" column="cname" />
</resultMap>
<select id="findAllByName" parameterType="string" resultMap="courseMap">
select c.cid,c.cname
from student3 s inner join middles m
on s.sid = m.msid
inner join coures c
on m.mcid = c.cid
and s.sname = #{name}
</select>
</mapper>
测试类:
package manybymany;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import util.MybatisUtil;
/**
* 持久层
* @author user
*
*/
public class StudentCourseDao {
/**
* 查询学生选学啦哪些课程
*@param name 学生的名字
*/
public List<Course> findAllByName(String name) {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
return sqlSession.selectList("courseNamespace.findAllByName", name);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
MybatisUtil.closeSqlSession();
}
return null;
}
/**
* 查询课程由哪些学生选修
*@param name 课程名称
*/
public List<Student> findAllByCourseName(String name) {
SqlSession sqlSession = null;
try {
sqlSession = MybatisUtil.getSqlSession();
return sqlSession.selectList("studentNamespace.findAllByCourseName", name);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
MybatisUtil.closeSqlSession();
}
return null;
}
public static void main(String[] args) {
StudentCourseDao dao = new StudentCourseDao();
// List<Course> courseList = dao.findAllByName("yss");
// System.out.print("yss选修了"+courseList.size()+"门课程");
// for (Course course : courseList) {
// System.out.print("课程信息 = "+course.getName()+" ");
// }
List<Student> studengList = dao.findAllByCourseName("java");
System.out.print("选修了java课程的人有"+studengList.size()+"个人");
for (Student student : studengList) {
System.out.println("姓名"+student.getName());
}
}
}
以上就是mybayis一对一、一对多、多对多的sql语句的写法,项目实战中会经常遇到