在数据库中创建员工表,表中存储员工编号、员工姓名、员工年龄、员工职位、员工工资等信息,创建Java项目,应用MyBatis框架,对员工表进行如下操作: 1、 分别根据id、根据姓名查询员工信息、查询所有员工信息 2、 新增员工信息、根据id修改员工信息、根据id删除员工信息。
时间: 2023-12-22 22:06:16 浏览: 123
1、创建员工表
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`age` int(11) NOT NULL,
`position` varchar(50) NOT NULL,
`salary` double NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、创建Employee实体类
public class Employee {
private int id;
private String name;
private int age;
private String position;
private double salary;
// getter and setter methods
}
3、创建EmployeeMapper接口
public interface EmployeeMapper {
List<Employee> getAllEmployees();
Employee getEmployeeById(int id);
Employee getEmployeeByName(String name);
void addEmployee(Employee employee);
void updateEmployee(Employee employee);
void deleteEmployee(int id);
}
4、创建EmployeeMapper.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">
<mapper namespace="com.example.mapper.EmployeeMapper">
<select id="getAllEmployees" resultType="com.example.entity.Employee">
SELECT * FROM employee
</select>
<select id="getEmployeeById" parameterType="int" resultType="com.example.entity.Employee">
SELECT * FROM employee WHERE id = #{id}
</select>
<select id="getEmployeeByName" parameterType="String" resultType="com.example.entity.Employee">
SELECT * FROM employee WHERE name = #{name}
</select>
<insert id="addEmployee" parameterType="com.example.entity.Employee">
INSERT INTO employee (name, age, position, salary)
VALUES (#{name}, #{age}, #{position}, #{salary})
</insert>
<update id="updateEmployee" parameterType="com.example.entity.Employee">
UPDATE employee SET name=#{name}, age=#{age}, position=#{position}, salary=#{salary}
WHERE id=#{id}
</update>
<delete id="deleteEmployee" parameterType="int">
DELETE FROM employee WHERE id=#{id}
</delete>
</mapper>
5、创建MyBatis配置文件
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mapper/EmployeeMapper.xml"/>
</mappers>
</configuration>
6、测试代码
public class TestEmployeeMapper {
public static void main(String[] args) {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(TestEmployeeMapper.class.getClassLoader().getResourceAsStream("mybatis-config.xml"));
SqlSession sqlSession = sqlSessionFactory.openSession();
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
// test getAllEmployees
List<Employee> employees = employeeMapper.getAllEmployees();
for (Employee employee : employees) {
System.out.println(employee.getName());
}
// test getEmployeeById
Employee employee = employeeMapper.getEmployeeById(1);
System.out.println(employee.getName());
// test getEmployeeByName
employee = employeeMapper.getEmployeeByName("张三");
System.out.println(employee.getId());
// test addEmployee
employee = new Employee();
employee.setName("李四");
employee.setAge(23);
employee.setPosition("职员");
employee.setSalary(5000.0);
employeeMapper.addEmployee(employee);
// test updateEmployee
employee = employeeMapper.getEmployeeById(2);
employee.setSalary(6000.0);
employeeMapper.updateEmployee(employee);
// test deleteEmployee
employeeMapper.deleteEmployee(3);
sqlSession.commit();
sqlSession.close();
}
}
以上就是对员工表的增删改查操作,通过MyBatis框架进行实现。
阅读全文