在数据库中创建员工表,表中存储员工编号、员工姓名、员工年龄、员工职位、员工工资等信息,创建Java项目,应用MyBatis框架,对员工表进行如下操作: 1、 分别根据id、根据姓名查询员工信息、查询所有员工信息 2、 新增员工信息、根据id修改员工信息、根据id删除员工信息。
时间: 2023-12-22 20:06:28 浏览: 141
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 AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2、定义实体类
public class Employee {
private int id;
private String name;
private int age;
private String position;
private double salary;
//getter setter
}
3、定义Mapper接口
public interface EmployeeMapper {
Employee getEmployeeById(int id);
List<Employee> getEmployeeByName(String name);
List<Employee> getAllEmployee();
void addEmployee(Employee employee);
void updateEmployee(Employee employee);
void deleteEmployeeById(int id);
}
4、编写Mapper.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="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>
<select id="getAllEmployee" resultType="com.example.entity.Employee">
select * from employee
</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="deleteEmployeeById" parameterType="int">
delete from employee where id=#{id}
</delete>
</mapper>
5、使用MyBatis进行操作
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
//根据id查询员工信息
Employee employee = mapper.getEmployeeById(1);
System.out.println(employee);
//根据姓名查询员工信息
List<Employee> employees = mapper.getEmployeeByName("张三");
System.out.println(employees);
//查询所有员工信息
List<Employee> allEmployees = mapper.getAllEmployee();
System.out.println(allEmployees);
//新增员工信息
Employee newEmployee = new Employee();
newEmployee.setName("李四");
newEmployee.setAge(25);
newEmployee.setPosition("开发工程师");
newEmployee.setSalary(8000.0);
mapper.addEmployee(newEmployee);
session.commit();
//根据id修改员工信息
Employee updateEmployee = mapper.getEmployeeById(2);
updateEmployee.setName("王五");
updateEmployee.setAge(26);
updateEmployee.setPosition("测试工程师");
updateEmployee.setSalary(9000.0);
mapper.updateEmployee(updateEmployee);
session.commit();
//根据id删除员工信息
mapper.deleteEmployeeById(3);
session.commit();
session.close();
}
阅读全文