mybatis映射枚举类型
程序员文章站
2022-04-23 15:57:04
...
/*
* 测试枚举映射
* 定义枚举
* 定义实体类,在实体类中引用枚举
* 建数据库表
* 创建映射器
* 接口
* 配置文件,需要制定枚举类型的内建的处理器
* 在全局配置文件中定义别名,关联映射器的配置文件
*
*
*
*
*
* */
- 定义枚举
- 定义实体类,引用枚举
- 创建数据表
- 创建映射器
- 接口
- 配置文件,需要制定枚举类型的内建的处理器
- 在全局配置文件中定义别名,关联映射器的配置文件
------------------------------------------------------------------------------------------
开始
- 定义枚举
-
package com.test.mybatis.xmlYingSheQi3; public enum SexEnum { MAIL(0, "男"), FEMALE(1, "女"); private int id; private String name; 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; } private SexEnum(int id, String name) { this.id = id; this.name = name; } public SexEnum getSexById(int id) { SexEnum sex = null; SexEnum[] values = SexEnum.values(); for (int i = 0; i < values.length; i++) { if(values[i].getId() == id) { sex = values[i]; break; } } return sex; } }
-
- 定义实体类,引用枚举
-
package com.test.mybatis.xmlYingSheQi3; //@Alias("people") public class People { private int id; private String name; private SexEnum sex; 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 SexEnum getSex() { return sex; } public void setSex(SexEnum sex) { this.sex = sex; } public People(int id, String name, SexEnum sex) { super(); this.id = id; this.name = name; this.sex = sex; } public People() { super(); } public People(int id, String name) { super(); this.id = id; this.name = name; } @Override public String toString() { return "People [id=" + id + ", name=" + name + ", sex=" + sex.getName() + "]"; } }
-
- 创建数据表
-
`tmptest` create table people( id int, name varchar(45), sex int ) drop table people; select * from people; insert into people values(1001, '鏉庣櫧', 0); insert into people values(1002, '鏉庢檽闈�', 1); delete from people where id = 1001;
-
- 创建映射器
- 接口
-
package com.test.mybatis.xmlYingSheQi3; import java.util.List; public interface PeopleMapper { List<People> getAllPeople(); People getPeopleByID(int id); int updatePeople(People people); int deletePeople(int id); int addPeople(People people); }
-
- 配置文件,需要制定枚举类型的内建的处理器
-
<?xml version="1.0" encoding="UTF-8"?> <!-- 约束条件 mapper namespace sql --> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.test.mybatis.xmlYingSheQi3.PeopleMapper"> <resultMap type="people" id="peopleMapper"> <result property="id" column="id"/> <result property="name" column="name"/> <result property="sex" column="sex" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"/> </resultMap> <select id="getPeopleByID" parameterType="int" resultType="people" resultMap="peopleMapper"> select id, name, sex from people where id = #{id} </select> <select id="getAllPeople" resultType="people" resultMap="peopleMapper"> select id, name, sex from people </select> <insert id="addPeople" parameterType="people"> insert into people(id, name, sex) values(#{id}, #{name}) </insert> <update id="updatePeople" parameterType="people"> update people set name = #{name} where id = #{id} </update> <delete id="deletePeople" parameterType="int"> delete from people where id = #{id} </delete> </mapper>
-
- 接口
- 在全局配置文件中定义别名,关联映射器的配置文件
-
<?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 alias="people" type="com.test.mybatis.xmlYingSheQi3.People"/> </typeAliases> <!-- 这是声明类型处理器的地方 --> <!-- 数据库环境 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 事务管理器 ,此处采用JDBC事务管理器模式 --> <!-- 配置数据库, POOLED 表示当前使用mybatis内部提供的连接池的方式 --> <dataSource type="POOLED"> <!-- JDBC的属性信息 --> <property name="driver" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/tmpTest?serverTimezone=UTC" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <!-- 映射文件 --> <!-- 代表引入哪些映射器 --> <mappers> <mapper resource="com/test/mybatis/xmlYingSheQi3/PeopleMapper.xml"/> </mappers> </configuration>
-