DAO层 //01.查询所有的学生信息 public List<studentinfo> findAll(); //02.根据学生学号查询特定学生对象 public studentinfo getStudentById(int stuid); //添加学生 public int addStudent(studentinfo stu); //修改 public int updateStudent(studentinfo info); //删除 public int deleteStudent(int stuid); //模糊 查询 public List<studentinfo> findLike(studentinfo info); //多条件查询Map public List<studentinfo> findStudentsByCondition(Map<String,Object> map); public List<studentinfo> findStudentsByConditionMutliArgs(String stuName,int stuAge); //智能白标签if public List<studentinfo> findByIf(studentinfo stu); //智能标签choose public List<studentinfo> findByChoose(studentinfo stu); //智能 标签foreach public List<studentinfo> findByForeachArray(int[] ids); //智能标签foreach List<Integer> public List<studentinfo> findByForeachList(List<Integer> list); //智能标签 foreach List(studentinfo) public List<studentinfo> findByForeachStudent(List<studentinfo> list); 小配置<?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="cn.happy.DAO.IStudentInfoDAO"> <!--SQL标签 id:唯一锁定到SQL标识 paramenterType:SQL语句的入参 可以省略 resultType: 增删除操作:不能 写 查询:单个实体的类型 --> <sql id="columns"> stuId,stuName,stuAge,stuDate </sql> <select id="findAll" resultType="cn.happy.entity.studentinfo"> /*SQL文:SQL语句*/ select <include refid="columns"></include>from studentinfo </select> <!--按主键查询--> <select id="getStudentById" resultType="cn.happy.entity.studentinfo"> select * from studentinfo WHERE stuid=#{stuId} </select> <insert id="addStudent"> INSERT INTO studentinfo(stuName,stuAge,stuDate) VALUE (#{stuName},#{stuAge},#{stuDate}) </insert> <update id="updateStudent"> UPDATE studentinfo SET stuName=#{stuName} where stuId=#{stuId} </update> <delete id="deleteStudent"> DELETE from studentinfo where stuId=#{stuId} </delete> <!-- 模糊查询 --> <select id="findLike" resultType="cn.happy.entity.studentinfo"> SELECT * FROM studentinfo where stuName LIKE '%' #{stuName} '%' and stuAge>#{stuAge} </select> <!--多条件查询--> <select id="findStudentsByCondition" resultType="cn.happy.entity.studentinfo"> SELECT * FROM studentinfo where stuname LIKE '%' #{stuName} '%' AND stuAge>#{stuAge} </select> <!--多条件查询使用 索引--> <select id="findStudentsByConditionMutliArgs" resultType="cn.happy.entity.studentinfo"> SELECT * FROM studentinfo WHERE stuname LIKE '%' #{0} '%' AND stuAge>#{1} </select> <!--智能标签if--> <select id="findByIf" resultType="cn.happy.entity.studentinfo"> SELECT * FROM studentinfo WHERE 1=1 <if test="stuName!=null"> AND stuName LIKE '%' #{stuName} '%' </if> <if test="stuAge!=null"> AND stuAge>#{stuAge} </if> </select> <!--智能 标签choose--> <select id="findByChoose" resultType="cn.happy.entity.studentinfo"> SELECT * FROM studentinfo <where> <choose> <when test="stuName!=null"> AND stuName LIKE '%' #{stuName} '%' </when> <when test="stuAge!=null"> AND stuAge>#{stuAge} </when> <otherwise> AND 1=2 </otherwise> </choose> </where> </select> <!-- 智能标签foreach Array--> <select id="findByForeachArray" resultType="cn.happy.entity.studentinfo"> SELECT * FROM studentinfo <where> <if test="array.length>0"> stuId IN <foreach collection="array" open="(" close=")" separator="," item="stuno"> #{stuno} </foreach> </if> </where> </select> <!-- 智能标签foreach List--> <select id="findByForeachStudent" resultType="cn.happy.entity.studentinfo"> SELECT * FROM studentinfo <where> <if test="list.size>0"> stuId IN <foreach collection="list" open="(" close=")" separator="," item="stu"> #{stu.stuId} </foreach> </if> </where> </select> <!--智能标签Foreach list--> <select id="findByForeachList" resultType="cn.happy.entity.studentinfo"> select * from studentinfo <where> <if test="list.size>0"> stuid IN <foreach collection="list" open="(" close=")" separator="," item="stuno"> #{stuno} </foreach> </if> </where> </select> </mapper>一对多DAOpublic Dept getEmpsByDeptNo(int deptNo); 小配置<?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="cn.happy.DAO.IDeptDAO"> <!--根据部门编号,查询部门对象,对象里面包含着员工的集合--> <resultMap id="deptMapper" type="cn.happy.entity.Dept"> <id column="deptNo" property="deptNo"></id> <result column="deptName" property="deptName"/> <collection property="emps" ofType="cn.happy.entity.Emp"> <id column="empNo" property="empNo"></id> <result column="empName" property="empName"/> </collection> </resultMap> <select id="getEmpsByDeptNo" resultMap="deptMapper"> select dept.deptNo,deptName,empNo,empName from dept,emp where dept.deptNo=emp.deptNo and dept.deptNo=#{deptNo} </select> </mapper>单侧@Test public void testAll(){ String path="MyBatis-config.xml"; InputStream is= null; try { is = Resources.getResourceAsStream(path); SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); SqlSession session=factory.openSession(); List<studentinfo> list = session.selectList("findAll"); for (studentinfo info:list) { System.out.println(info.getStuName()); } session.close(); } catch (IOException e) { e.printStackTrace(); } } @Test //02.根据主键查询单个对象 public void add(){ String path="MyBatis-config.xml"; InputStream is= null; try { is = Resources.getResourceAsStream(path); SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); SqlSession session=factory.openSession(); IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class); studentinfo stu=new studentinfo(); stu.setStuName("太阳"); stu.setStuAge(33); stu.setStuDate(new Date()); dao.addStudent(stu); session.commit(); System.out.println("ok!"); session.close(); } catch (IOException e) { e.printStackTrace(); } } //修改 @Test public void xg() { SqlSession session=myBatis.getSqlSession(); IStudentInfoDAO dao=session.getMapper(IStudentInfoDAO.class); studentinfo stu=new studentinfo(); stu.setStuName("星星"); stu.setStuId(2); dao.updateStudent(stu); session.commit(); System.out.println("完美"); session.close(); } //删除 @Test public void xc() throws Exception { SqlSession session=myBatis.getSqlSession(); IStudentInfoDAO dao=session.getMapper(IStudentInfoDAO.class); dao.deleteStudent(3); session.commit(); System.out.println("ok"); session.close(); } //模糊查询 @Test public void mh() throws Exception { SqlSession session=myBatis.getSqlSession(); IStudentInfoDAO dao=session.getMapper(IStudentInfoDAO.class); studentinfo stu=new studentinfo(); stu.setStuName("太阳"); stu.setStuAge(1); List<studentinfo> list=dao.findLike(stu); for (studentinfo info:list ) { System.out.println(info.getStuName()); } session.close(); } //多条件查询 @Test public void duo() throws Exception { SqlSession session=myBatis.getSqlSession(); IStudentInfoDAO dao=session.getMapper(IStudentInfoDAO.class); Map<String,Object> map=new HashMap<String,Object>(); map.put("stuName","阳"); map.put("stuAge","20"); List<studentinfo> list=dao.findStudentsByCondition(map); for (studentinfo info:list ) { System.out.println(info.getStuName()); } session.close(); } @Test public void duoMulti() throws Exception { SqlSession session= myBatis.getSqlSession(); IStudentInfoDAO dao=session.getMapper(IStudentInfoDAO.class); List<studentinfo> list=dao.findStudentsByConditionMutliArgs("龙",20); for (studentinfo info:list ) { System.out.println(info.getStuName()); } session.close(); } //智能标签if @Test public void Testif() throws Exception { SqlSession session= myBatis.getSqlSession(); IStudentInfoDAO dao=session.getMapper(IStudentInfoDAO.class); studentinfo stu=new studentinfo(); stu.setStuName("龙"); stu.setStuAge(20); List<studentinfo> list=dao.findByIf(stu); for (studentinfo info:list ) { System.out.println(info.getStuName()); } session.close(); } @Test public void Testchoose() throws Exception { SqlSession session= myBatis.getSqlSession(); IStudentInfoDAO dao=session.getMapper(IStudentInfoDAO.class); studentinfo stu=new studentinfo(); stu.setStuName("龙"); stu.setStuAge(20); List<studentinfo> list=dao.findByChoose(stu); for (studentinfo info:list ) { System.out.println(info.getStuName()); } session.close(); } //智能标签ForeachArray @Test public void testForeachArray() throws Exception { SqlSession session= myBatis.getSqlSession(); IStudentInfoDAO dao=session.getMapper(IStudentInfoDAO.class); int[] ids={1,2}; List<studentinfo> list=dao.findByForeachArray(ids); for (studentinfo info:list ) { System.out.println(info.getStuName()); } session.close(); } //智能标签Foreach List<studentinfo> @Test public void testForeachstudentinfo() throws Exception { SqlSession session= myBatis.getSqlSession(); IStudentInfoDAO dao=session.getMapper(IStudentInfoDAO.class); List<studentinfo> list=new ArrayList<studentinfo>(); studentinfo s1=new studentinfo(); s1.setStuId(2); studentinfo s2=new studentinfo(); s2.setStuId(3); list.add(s1); list.add(s2); List<studentinfo> list2=dao.findByForeachStudent(list); for (studentinfo info:list2 ) { System.out.println(info.getStuName()); } session.close(); } //智能标签Foreach List<Intager> @Test public void testForeachList() throws Exception { SqlSession session= myBatis.getSqlSession(); IStudentInfoDAO dao=session.getMapper(IStudentInfoDAO.class); List<Integer> list=new ArrayList<Integer>(); list.add(2); list.add(3); List<studentinfo> list2=dao.findByForeachList(list); for (studentinfo info:list2 ) { System.out.println(info.getStuName()); } session.close(); } 工具类 @Test public void gj() throws Exception { SqlSession session=myBatis.getSqlSession(); List<studentinfo> list = session.selectList("findAll"); for (studentinfo info:list ) { System.out.println(info.getStuName()); } } //07.多表连接查询 一对多 单条SQL @Test public void testOneToMany(){ Dept deptNo = myBatis.getSqlSession().getMapper(IDeptDAO.class).getEmpsByDeptNo(1); /*SqlSession session= myBatis.getSqlSession(); IDeptDAO dao = session.getMapper(IDeptDAO.class); Dept dept = dao.getEmpsByDeptNo(1);*/ System.out.println(deptNo.getDeptName()); for (Emp emp:deptNo.getEmps()) { System.out.println(emp.getEmpName()); } }
智能 标签 一对多
最新推荐文章于 2023-02-22 22:26:11 发布