赞
踩
一:实验目的
1、了解Mybatis基础知识;
2、理解Mybatis工作原理
3、掌握Mybatis的注解式开发。
二:实验内容
1)在mybatis数据库中创建:
车间基本表:dept(dno,dname,leader),其属性分别表示车间编号、车间名和车间主任;
CREATE TABLE dept(
dno CHAR(8) PRIMARY KEY NOT NULL,
dname VARCHAR(20),
leader VARCHAR(20)
);
职工基本表:empl(eno,ename,sex,entryDate,dno),其属性分别表示职工号、姓名、性别、入职日期和所在车间的编号.
CREATE TABLE empl(
eno CHAR(8),
ename VARCHAR(20),
sex VARCHAR(20),
entryDate DATE,
dno CHAR(8),
FOREIGN KEY(dno) REFERENCES dept(dno)
);
创建Dept类:
- package pojo;
-
- import java.util.List;
-
- public class Dept {
-
- private String dno; //车间编号
- private String dname; //车间名
- private String leader; //车间主任
-
- private List<Empl> emplList;//车间所有职工信息
-
- @Override
- public String toString() {
- return "Dept{" +
- "dno='" + dno + '\'' +
- ", dname='" + dname + '\'' +
- ", leader='" + leader + '\'' +
- ", emplList=" + emplList +
- '}';
- }
-
- public String getDno() {
- return dno;
- }
-
- public void setDno(String dno) {
- this.dno = dno;
- }
-
- public String getDname() {
- return dname;
- }
-
- public void setDname(String dname) {
- this.dname = dname;
- }
-
- public String getLeader() {
- return leader;
- }
-
- public void setLeader(String leader) {
- this.leader = leader;
- }
-
- public List<Empl> getEmplList() {
- return emplList;
- }
-
- public void setEmplList(List<Empl> emplList) {
- this.emplList = emplList;
- }
- }
创建Empl类:
- package pojo;
-
- public class Empl {
-
- private String eno; //职工号
- private String ename; //姓名
- private String sex; //性别
- private String entryDate;//入职日期
- private String dno; //车间编号
-
- @Override
- public String toString() {
- return "Empl{" +
- "eno='" + eno + '\'' +
- ", ename='" + ename + '\'' +
- ", sex='" + sex + '\'' +
- ", entryDate='" + entryDate + '\'' +
- ", dno='" + dno + '\'' +
- '}';
- }
-
- public String getEno() {
- return eno;
- }
-
- public void setEno(String eno) {
- this.eno = eno;
- }
-
- public String getEname() {
- return ename;
- }
-
- public void setEname(String ename) {
- this.ename = ename;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- public String getEntryDate() {
- return entryDate;
- }
-
- public void setEntryDate(String entryDate) {
- this.entryDate = entryDate;
- }
-
- public String getDno() {
- return dno;
- }
-
- public void setDno(String dno) {
- this.dno = dno;
- }
- }
创建DeptMapper接口:
- package dao;
-
- import org.apache.ibatis.annotations.*;
- import pojo.Dept;
-
- public interface DeptMapper {
-
- //向车间表添加3条记录;
- @Insert("INSERT INTO dept VALUE('001', '一号车间', '李富贵'),"+
- "('002', '二号车间', '王有才'),"+
- "('003', '三号车间', '吴签')"
- )
- public int insertDept();
-
- //根据车间名查询车间信息及该车间的所有职工信息。即一对多关系
- @Select("SELECT * FROM dept WHERE dname=#{dname}")
- @Results({
- @Result(id = true, property = "dno", column = "dno"),
- @Result(property = "dname", column = "dname"),
- @Result(property = "leader", column = "leader"),
- @Result(property = "emplList",
- column = "dno",
- many = @Many(select = "dao.EmplMapper.selectEmplByDno"))
- })
- public Dept selectAllByDname(String dno);
-
-
-
- }
创建Empl接口;
- package dao;
-
- import org.apache.ibatis.annotations.*;
- import pojo.Dept;
- import pojo.Empl;
-
- import java.util.List;
-
- public interface EmplMapper {
-
- //向职工表添加3条记录;
- @Insert("INSERT INTO empl VALUE('1', '张三', '男', '2020-01-01', '001'),"+
- "('2', '周杰伦', '男', '2020-07-01', '002'),"+
- "('3', '王非', '女', '2020-10-01', '003')")
- public int insertEmpl();
-
- //基于职工号查询职工信息;
- @Select("select * from empl where eno=#{eno}")
- public Empl selectEmplByEno(String eno);
-
- //基于姓名+性别查询职工信息;
- @Select("SELECT * FROM empl WHERE ename=#{ename} and sex=#{sex}")
- public Empl selectEmplByEnameAndSex(Empl empl);
-
- //修改指定员工的车间号信息;
- @Update("UPDATE empl SET dno=#{dno} WHERE ename=#{ename}")
- public int updateEmplDnoByEname(Empl empl);
-
- //删除指定员工信息;
- @Delete("delete from empl where ename=#{ename}")
- public int deleteEmplByEname(String ename);
-
- //根据车间名查询车间信息及该车间的所有职工信息。即一对多关系
- @Select("select * from empl where dno=#{dno}")
- @Results({
- @Result(id = true, property = "eno", column = "eno"),
- @Result(property = "ename", column = "ename"),
- @Result(property = "sex", column = "sex"),
- @Result(property = "entryDate", column = "entryDate")
- })
- public List<Empl> selectEmplByDno(String dno);
-
- }
创建MyBatisUtils工具类:
- package utils;
-
- 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 java.io.IOException;
- import java.io.Reader;
-
- public class MyBatisUtils {
-
- private static SqlSessionFactory sqlSessionFactory = null;
- static {
- //创建sqlSessionFactory
- Reader reader = null;
- try {
- reader = Resources.getResourceAsReader("mybatis-config.xml");
- sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public static SqlSession getSession(){
- return sqlSessionFactory.openSession();
- }
-
- }
mabatis-config.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>
- <!--加载配置-->
- <!--加载类路径下的属性文件-->
- <properties resource="jdbc.properties"/>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC"></transactionManager>
- <!--数据库连接相关配置,db.properties文件中的内容-->
- <dataSource type="POOLED">
- <property name="driver" value="${mysql.driver}"/>
- <property name="url" value="${mysql.url}"/>
- <property name="username" value="${mysql.username}"/>
- <property name="password" value="${mysql.password}"/>
- </dataSource>
- </environment>
- </environments>
-
- <mappers>
- <mapper class="dao.DeptMapper"></mapper>
- <mapper class="dao.EmplMapper"></mapper>
- </mappers>
-
- </configuration>
jdbc.properties文件(用户名和密码需要改为你们自己的):
- mysql.driver=com.mysql.cj.jdbc.Driver
- mysql.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&\
- characterEncoding=utf8&useUnicode=true&useSSL=false
- mysql.username=root
- mysql.password=123456
2)基于注解式实现以下功能:
1、向车间表、职工表分别添加3条记录;
2、基于职工号查询职工信息;
3、基于姓名+性别查询职工信息;
4、修改指定员工的车间号信息;
5、删除指定员工信息;
6、根据车间名查询车间信息及该车间的所有职工信息。
# 创建DeptMapperTest测试类:
- package dao;
-
- import junit.framework.TestCase;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import pojo.Dept;
- import utils.MyBatisUtils;
-
- public class DeptMapperTest {
-
- @Test
- //向车间表添加3条记录;
- public void testInsertDept() {
-
- SqlSession session = MyBatisUtils.getSession();
- DeptMapper deptMapper = session.getMapper(DeptMapper.class);
- int i = deptMapper.insertDept();
-
- if(i != 0){
- System.out.println("添加成功!");
- }else{
- System.out.println("添加失败!");
- }
-
- session.commit(); //记得要写提交事务语句,要不然数据不能插入数据库
- session.close();
- }
-
- @Test
- //根据车间名查询车间信息及该车间的所有职工信息。
- public void selectAllByDname(){
- SqlSession session = MyBatisUtils.getSession();
- DeptMapper deptMapper = session.getMapper(DeptMapper.class);
- Dept dept = deptMapper.selectAllByDname("一号车间");
- System.out.println(dept);
- session.close();
- }
-
- }
# 创建EmplMapperTest测试类:
- package dao;
- import org.apache.ibatis.session.SqlSession;
- import org.junit.Test;
- import pojo.Dept;
- import pojo.Empl;
- import utils.MyBatisUtils;
-
- public class EmplMapperTest {
-
- @Test
- //向职工表添加3条记录;
- public void insertEmpl(){
- SqlSession session = MyBatisUtils.getSession();
- EmplMapper emplMapper = session.getMapper(EmplMapper.class);
- int i = emplMapper.insertEmpl();
- if(i != 0){
- System.out.println("添加成功!");
- }else {
- System.out.println("添加失败!");
- }
- session.commit(); //记得要写提交事务语句,要不然数据不能插入数据库
- session.close();
- }
-
- @Test
- //基于职工号查询职工信息;
- public void SelectEmplByEno() {
- SqlSession session = MyBatisUtils.getSession();
- EmplMapper emplMapper = session.getMapper(EmplMapper.class);
- Empl empl =emplMapper.selectEmplByEno("1");
- System.out.println(empl);
- session.close();
- }
-
- @Test
- //基于姓名+性别查询职工信息;
- public void selectEmplByEnameAndSex(){
- SqlSession session = MyBatisUtils.getSession();
- EmplMapper emplMapper = session.getMapper(EmplMapper.class);
- Empl empl = new Empl(); //两个输入条件的时候需要创建一个模拟对象来保存查询条件,否则会报错
- empl.setEname("周杰伦");
- empl.setSex("男");
- Empl emplRes =emplMapper.selectEmplByEnameAndSex(empl);
- System.out.println(emplRes);
- session.close();
- }
-
- @Test
- //修改指定员工的车间号信息;
- public void updateEmplDnoByEname(){
- SqlSession session = MyBatisUtils.getSession();
- EmplMapper emplMapper = session.getMapper(EmplMapper.class);
- Empl empl = new Empl(); //两个输入条件的时候需要创建一个模拟对象来保存查询条件,否则会报错
- empl.setDno("001"); //需要修改的车间号
- empl.setEname("张三"); //指定员工
- int i =emplMapper.updateEmplDnoByEname(empl);
- if(i != 0){
- System.out.println("修改成功!");
- }else {
- System.out.println("修改失败!");
- }
- session.commit(); //记得要写提交事务语句,要不然数据不能插入数据库
- session.close();
- }
-
- @Test
- //删除指定员工信息;
- public void deleteEmplByEname(){
- SqlSession session = MyBatisUtils.getSession();
- EmplMapper emplMapper = session.getMapper(EmplMapper.class);
- int i =emplMapper.deleteEmplByEname("周杰伦");
- if(i != 0){
- System.out.println("删除成功!");
- }else {
- System.out.println("删除失败!");
- }
- session.commit(); //记得要写提交事务语句,要不然删除不了数据
- session.close();
- }
-
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。