当前位置:   article > 正文

实验7 Mybatis注解式开发_mybatis注解开发实验报告

mybatis注解开发实验报告

一:实验目的

1、了解Mybatis基础知识;

2、理解Mybatis工作原理

3、掌握Mybatis的注解式开发。

二:实验内容

1)在mybatis数据库中创建:

车间基本表:dept(dnodnameleader),其属性分别表示车间编号、车间名和车间主任;

CREATE TABLE dept(
    dno CHAR(8) PRIMARY KEY NOT NULL,
    dname VARCHAR(20),
    leader VARCHAR(20)
);

职工基本表:empl(enoenamesexentryDatedno),其属性分别表示职工号、姓名、性别、入职日期和所在车间的编号.

CREATE TABLE empl(
    eno CHAR(8),
    ename VARCHAR(20),
    sex VARCHAR(20),
    entryDate DATE,
    dno CHAR(8),
    FOREIGN KEY(dno) REFERENCES dept(dno)
);

创建Dept类:

  1. package pojo;
  2. import java.util.List;
  3. public class Dept {
  4. private String dno; //车间编号
  5. private String dname; //车间名
  6. private String leader; //车间主任
  7. private List<Empl> emplList;//车间所有职工信息
  8. @Override
  9. public String toString() {
  10. return "Dept{" +
  11. "dno='" + dno + '\'' +
  12. ", dname='" + dname + '\'' +
  13. ", leader='" + leader + '\'' +
  14. ", emplList=" + emplList +
  15. '}';
  16. }
  17. public String getDno() {
  18. return dno;
  19. }
  20. public void setDno(String dno) {
  21. this.dno = dno;
  22. }
  23. public String getDname() {
  24. return dname;
  25. }
  26. public void setDname(String dname) {
  27. this.dname = dname;
  28. }
  29. public String getLeader() {
  30. return leader;
  31. }
  32. public void setLeader(String leader) {
  33. this.leader = leader;
  34. }
  35. public List<Empl> getEmplList() {
  36. return emplList;
  37. }
  38. public void setEmplList(List<Empl> emplList) {
  39. this.emplList = emplList;
  40. }
  41. }

创建Empl类:

  1. package pojo;
  2. public class Empl {
  3. private String eno; //职工号
  4. private String ename; //姓名
  5. private String sex; //性别
  6. private String entryDate;//入职日期
  7. private String dno; //车间编号
  8. @Override
  9. public String toString() {
  10. return "Empl{" +
  11. "eno='" + eno + '\'' +
  12. ", ename='" + ename + '\'' +
  13. ", sex='" + sex + '\'' +
  14. ", entryDate='" + entryDate + '\'' +
  15. ", dno='" + dno + '\'' +
  16. '}';
  17. }
  18. public String getEno() {
  19. return eno;
  20. }
  21. public void setEno(String eno) {
  22. this.eno = eno;
  23. }
  24. public String getEname() {
  25. return ename;
  26. }
  27. public void setEname(String ename) {
  28. this.ename = ename;
  29. }
  30. public String getSex() {
  31. return sex;
  32. }
  33. public void setSex(String sex) {
  34. this.sex = sex;
  35. }
  36. public String getEntryDate() {
  37. return entryDate;
  38. }
  39. public void setEntryDate(String entryDate) {
  40. this.entryDate = entryDate;
  41. }
  42. public String getDno() {
  43. return dno;
  44. }
  45. public void setDno(String dno) {
  46. this.dno = dno;
  47. }
  48. }

创建DeptMapper接口:

  1. package dao;
  2. import org.apache.ibatis.annotations.*;
  3. import pojo.Dept;
  4. public interface DeptMapper {
  5. //向车间表添加3条记录;
  6. @Insert("INSERT INTO dept VALUE('001', '一号车间', '李富贵'),"+
  7. "('002', '二号车间', '王有才'),"+
  8. "('003', '三号车间', '吴签')"
  9. )
  10. public int insertDept();
  11. //根据车间名查询车间信息及该车间的所有职工信息。即一对多关系
  12. @Select("SELECT * FROM dept WHERE dname=#{dname}")
  13. @Results({
  14. @Result(id = true, property = "dno", column = "dno"),
  15. @Result(property = "dname", column = "dname"),
  16. @Result(property = "leader", column = "leader"),
  17. @Result(property = "emplList",
  18. column = "dno",
  19. many = @Many(select = "dao.EmplMapper.selectEmplByDno"))
  20. })
  21. public Dept selectAllByDname(String dno);
  22. }

创建Empl接口;

  1. package dao;
  2. import org.apache.ibatis.annotations.*;
  3. import pojo.Dept;
  4. import pojo.Empl;
  5. import java.util.List;
  6. public interface EmplMapper {
  7. //向职工表添加3条记录;
  8. @Insert("INSERT INTO empl VALUE('1', '张三', '男', '2020-01-01', '001'),"+
  9. "('2', '周杰伦', '男', '2020-07-01', '002'),"+
  10. "('3', '王非', '女', '2020-10-01', '003')")
  11. public int insertEmpl();
  12. //基于职工号查询职工信息;
  13. @Select("select * from empl where eno=#{eno}")
  14. public Empl selectEmplByEno(String eno);
  15. //基于姓名+性别查询职工信息;
  16. @Select("SELECT * FROM empl WHERE ename=#{ename} and sex=#{sex}")
  17. public Empl selectEmplByEnameAndSex(Empl empl);
  18. //修改指定员工的车间号信息;
  19. @Update("UPDATE empl SET dno=#{dno} WHERE ename=#{ename}")
  20. public int updateEmplDnoByEname(Empl empl);
  21. //删除指定员工信息;
  22. @Delete("delete from empl where ename=#{ename}")
  23. public int deleteEmplByEname(String ename);
  24. //根据车间名查询车间信息及该车间的所有职工信息。即一对多关系
  25. @Select("select * from empl where dno=#{dno}")
  26. @Results({
  27. @Result(id = true, property = "eno", column = "eno"),
  28. @Result(property = "ename", column = "ename"),
  29. @Result(property = "sex", column = "sex"),
  30. @Result(property = "entryDate", column = "entryDate")
  31. })
  32. public List<Empl> selectEmplByDno(String dno);
  33. }

创建MyBatisUtils工具类:

  1. package utils;
  2. import org.apache.ibatis.io.Resources;
  3. import org.apache.ibatis.session.SqlSession;
  4. import org.apache.ibatis.session.SqlSessionFactory;
  5. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  6. import java.io.IOException;
  7. import java.io.Reader;
  8. public class MyBatisUtils {
  9. private static SqlSessionFactory sqlSessionFactory = null;
  10. static {
  11. //创建sqlSessionFactory
  12. Reader reader = null;
  13. try {
  14. reader = Resources.getResourceAsReader("mybatis-config.xml");
  15. sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. public static SqlSession getSession(){
  21. return sqlSessionFactory.openSession();
  22. }
  23. }

mabatis-config.xml文件:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!--加载配置-->
  7. <!--加载类路径下的属性文件-->
  8. <properties resource="jdbc.properties"/>
  9. <environments default="development">
  10. <environment id="development">
  11. <transactionManager type="JDBC"></transactionManager>
  12. <!--数据库连接相关配置,db.properties文件中的内容-->
  13. <dataSource type="POOLED">
  14. <property name="driver" value="${mysql.driver}"/>
  15. <property name="url" value="${mysql.url}"/>
  16. <property name="username" value="${mysql.username}"/>
  17. <property name="password" value="${mysql.password}"/>
  18. </dataSource>
  19. </environment>
  20. </environments>
  21. <mappers>
  22. <mapper class="dao.DeptMapper"></mapper>
  23. <mapper class="dao.EmplMapper"></mapper>
  24. </mappers>
  25. </configuration>

jdbc.properties文件(用户名和密码需要改为你们自己的):

  1. mysql.driver=com.mysql.cj.jdbc.Driver
  2. mysql.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&\
  3. characterEncoding=utf8&useUnicode=true&useSSL=false
  4. mysql.username=root
  5. mysql.password=123456

2)基于注解式实现以下功能:

1、向车间表、职工表分别添加3条记录;

2、基于职工号查询职工信息;

3、基于姓名+性别查询职工信息;

4、修改指定员工的车间号信息;

5、删除指定员工信息;

6、根据车间名查询车间信息及该车间的所有职工信息。

# 创建DeptMapperTest测试类:

  1. package dao;
  2. import junit.framework.TestCase;
  3. import org.apache.ibatis.session.SqlSession;
  4. import org.apache.ibatis.session.SqlSessionFactory;
  5. import org.junit.After;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import pojo.Dept;
  9. import utils.MyBatisUtils;
  10. public class DeptMapperTest {
  11. @Test
  12. //向车间表添加3条记录;
  13. public void testInsertDept() {
  14. SqlSession session = MyBatisUtils.getSession();
  15. DeptMapper deptMapper = session.getMapper(DeptMapper.class);
  16. int i = deptMapper.insertDept();
  17. if(i != 0){
  18. System.out.println("添加成功!");
  19. }else{
  20. System.out.println("添加失败!");
  21. }
  22. session.commit(); //记得要写提交事务语句,要不然数据不能插入数据库
  23. session.close();
  24. }
  25. @Test
  26. //根据车间名查询车间信息及该车间的所有职工信息。
  27. public void selectAllByDname(){
  28. SqlSession session = MyBatisUtils.getSession();
  29. DeptMapper deptMapper = session.getMapper(DeptMapper.class);
  30. Dept dept = deptMapper.selectAllByDname("一号车间");
  31. System.out.println(dept);
  32. session.close();
  33. }
  34. }

# 创建EmplMapperTest测试类:

  1. package dao;
  2. import org.apache.ibatis.session.SqlSession;
  3. import org.junit.Test;
  4. import pojo.Dept;
  5. import pojo.Empl;
  6. import utils.MyBatisUtils;
  7. public class EmplMapperTest {
  8. @Test
  9. //向职工表添加3条记录;
  10. public void insertEmpl(){
  11. SqlSession session = MyBatisUtils.getSession();
  12. EmplMapper emplMapper = session.getMapper(EmplMapper.class);
  13. int i = emplMapper.insertEmpl();
  14. if(i != 0){
  15. System.out.println("添加成功!");
  16. }else {
  17. System.out.println("添加失败!");
  18. }
  19. session.commit(); //记得要写提交事务语句,要不然数据不能插入数据库
  20. session.close();
  21. }
  22. @Test
  23. //基于职工号查询职工信息;
  24. public void SelectEmplByEno() {
  25. SqlSession session = MyBatisUtils.getSession();
  26. EmplMapper emplMapper = session.getMapper(EmplMapper.class);
  27. Empl empl =emplMapper.selectEmplByEno("1");
  28. System.out.println(empl);
  29. session.close();
  30. }
  31. @Test
  32. //基于姓名+性别查询职工信息;
  33. public void selectEmplByEnameAndSex(){
  34. SqlSession session = MyBatisUtils.getSession();
  35. EmplMapper emplMapper = session.getMapper(EmplMapper.class);
  36. Empl empl = new Empl(); //两个输入条件的时候需要创建一个模拟对象来保存查询条件,否则会报错
  37. empl.setEname("周杰伦");
  38. empl.setSex("男");
  39. Empl emplRes =emplMapper.selectEmplByEnameAndSex(empl);
  40. System.out.println(emplRes);
  41. session.close();
  42. }
  43. @Test
  44. //修改指定员工的车间号信息;
  45. public void updateEmplDnoByEname(){
  46. SqlSession session = MyBatisUtils.getSession();
  47. EmplMapper emplMapper = session.getMapper(EmplMapper.class);
  48. Empl empl = new Empl(); //两个输入条件的时候需要创建一个模拟对象来保存查询条件,否则会报错
  49. empl.setDno("001"); //需要修改的车间号
  50. empl.setEname("张三"); //指定员工
  51. int i =emplMapper.updateEmplDnoByEname(empl);
  52. if(i != 0){
  53. System.out.println("修改成功!");
  54. }else {
  55. System.out.println("修改失败!");
  56. }
  57. session.commit(); //记得要写提交事务语句,要不然数据不能插入数据库
  58. session.close();
  59. }
  60. @Test
  61. //删除指定员工信息;
  62. public void deleteEmplByEname(){
  63. SqlSession session = MyBatisUtils.getSession();
  64. EmplMapper emplMapper = session.getMapper(EmplMapper.class);
  65. int i =emplMapper.deleteEmplByEname("周杰伦");
  66. if(i != 0){
  67. System.out.println("删除成功!");
  68. }else {
  69. System.out.println("删除失败!");
  70. }
  71. session.commit(); //记得要写提交事务语句,要不然删除不了数据
  72. session.close();
  73. }
  74. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/921996
推荐阅读
相关标签
  

闽ICP备14008679号