员工管理系统(Employee management system) 需求描述
实现思路 需求包括增删改查,可以使用mybatis框架快速满足需求
创建一个maven项目Ems(Employee management system)
修改pom.xml配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <?xml version="1.0" encoding="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > org.zdx-1</groupId > <artifactId > Ems</artifactId > <version > 1.0</version > <properties > <maven.compiler.source > 8</maven.compiler.source > <maven.compiler.target > 8</maven.compiler.target > </properties > <dependencies > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > 3.5.2</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 8.0.18</version > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.12</version > <scope > test</scope > </dependency > </dependencies > <build > <resources > <resource > <directory > src/main/java</directory > <includes > <include > **/*.properties</include > <include > **/*.xml</include > </includes > <filtering > true</filtering > </resource > </resources > </build > </project >
建立对应的文件目录
使用MySql建立数据库连接
新建数据库Ems,新建表employee,添加几条数据
1 2 3 4 5 6 7 8 9 10 11 12 use ems; create table employee( id int primary key auto_increment, name varchar(20), age int, position varchar(20) ); insert into employee (name, age, position) VALUES ('John', 20, 'developer'), ('Bob', 21, 'developer'), ('测试员01', 22, 'developer');
在resources中添加项目连接配置 mybatis-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE configuration PUBLIC "-//mybaties.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration > <properties resource ="db.properties" /> <environments default ="development" > <environment id ="development" > <transactionManager type ="JDBC" > </transactionManager > <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 resource ="mapper/UserMapper.xml" /> <mapper resource ="com/example/mapper/StudentMapper.xml" /> <mapper resource ="com/example/mapper/CustomerMapper.xml" /> </mappers > </configuration >
db.properties
1 2 3 4 mysql.driver =com.mysql.cj.jdbc.Driver mysql.url =jdbc:mysql://localhost:3306/Ems?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false mysql.username =root mysql.password =123456
编写一个utils文件MyBatisUtils.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.zdx.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 { try { Reader reader= Resources.getResourceAsReader("mybatis-config.xml" ); sqlSessionFactory=new SqlSessionFactoryBuilder ().build(reader); }catch (IOException e){ e.printStackTrace(); } } public static SqlSession getSession () { return sqlSessionFactory.openSession(); } }
这段代码是MyBatisUtils工具类的实现,用于提供SqlSession的获取方法,从而方便地进行数据库操作。具体来说:
这个类首先定义了一个静态的SqlSessionFactory变量sqlSessionFactory,用于存储SqlSessionFactory对象,它是MyBatis的核心对象,可以创建SqlSession实例。
在静态代码块中,通过Resources类的getResourceAsReader方法读取mybatis-config.xml配置文件,并使用SqlSessionFactoryBuilder的build方法构建SqlSessionFactory对象。
定义了一个公共的静态方法getSession,该方法返回一个SqlSession对象。通过调用sqlSessionFactory的openSession方法来创建SqlSession实例,从而可以执行MyBatis的SQL语句。
总结:这段代码的作用是通过MyBatisUtils工具类简化获取SqlSession的过程,使得开发者能够更加方便地进行数据库操作。
在pojo中创建employee实体类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 package com.zdx.pojo;public class employee { private int id; private String name; private int age; private String position; public int getId () { return id; } public void setId (int id) { this .id = id; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } public String getPosition () { return position; } public void setPosition (String position) { this .position = position; } public employee (int id, String name, int age, String position) { this .id = id; this .name = name; this .age = age; this .position = position; } public employee () { } }
在Mapper编写对应的查询条件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD mapper 3.0//EN" "htttp://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="com.zdx.mapper.EmployeeMapper" > <select id ="findAll" resultType ="com.zdx.pojo.Employee" resultMap ="EmployeeMap" > select * from employee where id=1 </select > <resultMap type ="com.zdx.pojo.Employee" id ="EmployeeMap" > <id property ="id" column ="id" /> <result property ="name" column ="name" /> <result property ="age" column ="age" /> <result property ="position" column ="position" /> </resultMap > <select id ="findById" parameterType ="int" resultMap ="EmployeeMap" > select * from employee where id=#{id} </select > <insert id ="insert" parameterType ="com.zdx.pojo.Employee" > insert into employee(name,age,position) values(#{name},#{age},#{position}) </insert > <update id ="update" parameterType ="com.zdx.pojo.Employee" > update employee set name=#{name},age=#{age},position=#{position} where id=#{id} </update > </mapper >
在test/java中编写测试类EmployeeTest 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 import com.zdx.pojo.Employee;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 org.junit.After;import org.junit.Before;import org.junit.Test;import java.io.IOException;import java.io.Reader;public class EmployeeTest { private SqlSessionFactory sqlSessionFactory; private SqlSession sqlSession; @Before public void init () { String resources = "mybatis-config.xml" ; Reader reader=null ; try { reader= Resources.getResourceAsReader(resources); SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder ().build(reader); sqlSession=sqlMapper.openSession(); } catch (IOException e) { e.printStackTrace(); } } @Test public void testSelectAll () { Employee employee=sqlSession.selectOne("com.zdx.mapper.EmployeeMapper.findAll" ); System.out.println(employee); } @Test public void testDelete () { Employee employee=new Employee (1 ,"张三" ,20 ,"manager" ); sqlSession.delete("com.zdx.mapper.EmployeeMapper.delete" ,employee); } @Test public void testInsert () { Employee employee=new Employee (1 ,"张三" ,20 ,"manager" ); sqlSession.insert("com.zdx.mapper.EmployeeMapper.insert" ,employee); } @Test public void testUpdate () { Employee employee=new Employee (1 ,"张三" ,20 ,"manager" ); sqlSession.update("com.zdx.mapper.EmployeeMapper.update" ,employee); System.out.println(employee.getName().toString()); } @After public void destroy () { sqlSession.commit(); sqlSession.close(); } }
其余实现基本同上面一致,故不再记录。
代码实现 代码仓库:https://github.com/zdx-1/JavaEE-Learn