使用mybatis实现一个动物园管理的增删改查
时间: 2024-02-28 18:54:12 浏览: 77
假设我们有一个动物园管理系统,需要对动物信息进行增删改查操作,我们可以使用MyBatis来实现。下面是一个使用XML方式配置MyBatis的示例:
1. 创建动物类Animal.java
```
public class Animal {
private int id;
private String name;
private String species;
private int age;
// 省略getter和setter方法
}
```
2. 创建动物表的数据库脚本
```
CREATE TABLE animal (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
species VARCHAR(50) NOT NULL,
age INT NOT NULL
);
```
3. 创建MyBatis的配置文件mybatis-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>
<typeAliases>
<typeAlias type="com.example.Animal" alias="Animal"/>
</typeAliases>
<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="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mapper/AnimalMapper.xml"/>
</mappers>
</configuration>
```
4. 创建AnimalMapper.xml,定义SQL语句和参数映射
```
<?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.AnimalMapper">
<resultMap id="AnimalResultMap" type="Animal">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="species" column="species"/>
<result property="age" column="age"/>
</resultMap>
<select id="getAnimalById" resultMap="AnimalResultMap" parameterType="int">
SELECT * FROM animal WHERE id = #{id}
</select>
<select id="getAllAnimals" resultMap="AnimalResultMap">
SELECT * FROM animal
</select>
<insert id="addAnimal" parameterType="Animal">
INSERT INTO animal (name, species, age)
VALUES (#{name}, #{species}, #{age})
</insert>
<update id="updateAnimal" parameterType="Animal">
UPDATE animal SET name = #{name}, species = #{species}, age = #{age} WHERE id = #{id}
</update>
<delete id="deleteAnimalById" parameterType="int">
DELETE FROM animal WHERE id = #{id}
</delete>
</mapper>
```
5. 创建AnimalMapper.java,定义接口方法
```
public interface AnimalMapper {
Animal getAnimalById(int id);
List<Animal> getAllAnimals();
void addAnimal(Animal animal);
void updateAnimal(Animal animal);
void deleteAnimalById(int id);
}
```
6. 编写测试类进行测试
```
public class AnimalTest {
private SqlSession sqlSession;
private AnimalMapper animalMapper;
@Before
public void setUp() throws Exception {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
sqlSession = sqlSessionFactory.openSession();
animalMapper = sqlSession.getMapper(AnimalMapper.class);
}
@Test
public void testGetAnimalById() {
Animal animal = animalMapper.getAnimalById(1);
System.out.println(animal.getName());
}
@Test
public void testGetAllAnimals() {
List<Animal> animals = animalMapper.getAllAnimals();
for (Animal animal : animals) {
System.out.println(animal.getName());
}
}
@Test
public void testAddAnimal() {
Animal animal = new Animal();
animal.setName("Tom");
animal.setSpecies("Cat");
animal.setAge(2);
animalMapper.addAnimal(animal);
sqlSession.commit();
}
@Test
public void testUpdateAnimal() {
Animal animal = animalMapper.getAnimalById(1);
animal.setName("Jerry");
animal.setSpecies("Mouse");
animalMapper.updateAnimal(animal);
sqlSession.commit();
}
@Test
public void testDeleteAnimalById() {
animalMapper.deleteAnimalById(1);
sqlSession.commit();
}
@After
public void tearDown() throws Exception {
sqlSession.close();
}
}
```
以上就是使用MyBatis进行动物园管理系统的增删改查操作的示例。
阅读全文