- EnumTypeHandler:该处理器将枚举值转换成对应的名称(字符串),这是默认的。
如果使用这种方式,程序不需要任何额外的配置。
项目0401EnumTypeHandler
- EnumOrdinalTypeHandler:该处理器将枚举值转换成对应的序号(整数)。
需要配置(不需要修改XML Mapper文件,只要修改 mybatis-config.xml配置文件即可)。
项目0402EnumOrdinalTypeHandler
▲ 同一个枚举类型,有个地方要保存枚举名称,有的要保存枚举序号:
项目0403枚举变态处理
<typeHandlers>
<typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler"
javaType="org.itcheng.app.domain.Season"
jdbcType="INTEGER"/>
typeHandlers>
<insert id="saveNews">
insert into news_inf values (null, #{title}, #{content},
#{happenSeason, typeHandler=org.apache.ibatis.type.EnumOrdinalTypeHandler,
javaType=org.itcheng.app.domain.Season}, #{recordSeason})
insert>
<select id="findNews" resultMap="newsMap">
select news_id id, news_title title, news_content content,
happen_season happenSeason, record_season recordSeason
from news_inf where news_id > #{id}
select>
<resultMap type="news" id="newsMap">
<result column="id" property="id"/>
<result column="happenSeason" property="happenSeason"
typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"
javaType="org.itcheng.app.domain.Season"/>
resultMap>
主类
package lee;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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.itcheng.app.dao.NewsMapper;
import org.itcheng.app.domain.News;
import org.itcheng.app.domain.Season;
public class NewsManager
{
// SqlSessionFactory应该是应用级别
private static SqlSessionFactory sqlSessionFactory;
public static void main(String[] args) throws IOException
{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 1. 创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2. 打开SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//EnumTypeHandler
// 保存消息
saveNews(sqlSession);
// 更新消息
// updateNews(sqlSession);
// 删除消息
// deleteNews(sqlSession);
// 查询消息
// selectNews(sqlSession);
}
public static void saveNews(SqlSession sqlSession)
{
News news = new News(null, "测试标题", "测试内容");
news.setHappenSeason(Season.SUMMER);
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
newsMapper.saveNews(news);
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
}
public static void updateNews(SqlSession sqlSession)
{
News news = new News(1, "修改的标题", "修改的内容");
news.setHappenSeason(Season.FALL);
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
newsMapper.updateNews(news);
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
}
public static void deleteNews(SqlSession sqlSession)
{
// SqlSession的insert、update、delete方法都只是去执行DML语句
// 因此这个三个方法并没有本质的区别:它们都只是调用PreparedStatement的executeUpdate()方法来执行DML语句。
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
newsMapper.deleteNews(2);
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
}
public static void selectNews(SqlSession sqlSession)
{
// 此处的NewsMapper只是一个接口
// MyBatis会使用JDK的动态代理为Mapper接口生成实现类
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
System.out.println(newsMapper.getClass());
List<?> list = newsMapper.findNews(-1);
System.out.println(list);
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
}
}
映射文件
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.itcheng.app.dao.NewsMapper">
<insert id="saveNews">
insert into news_inf values (null, #{title}, #{content}, #{happenSeason})
insert>
<update id="updateNews">
update news_inf set news_title = #{title}, news_content = #{content},
happen_season=#{happenSeason}
where news_id=#{id}
update>
<delete id="deleteNews">
delete from news_inf where news_id = #{xyz}
delete>
<select id="findNews" resultType="news">
select news_id id, news_title title, news_content content
happen_season happenSeason
from news_inf where news_id > #{id}
select>
mapper>
用户类
package org.itcheng.app.domain;
// 默认的别名是news
public class News
{
private Integer id;
private String title;
private String content;
// 它是一个枚举类型的属性
private Season happenSeason;//消息实在哪个季节发生的,数据库存字符串
//添加set和get方法,有参无参构造器
}
自定义枚举类
package org.itcheng.app.domain;
public enum Season
{
SPRING, SUMMER, FALL, WINTER;
}
表结构
create table news_inf
(
news_id int primary key auto_increment,
news_title varchar(255),
news_content varchar(255),
happen_season varchar(255)
);
主类
package lee;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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.itcheng.app.dao.NewsMapper;
import org.itcheng.app.domain.News;
import org.itcheng.app.domain.Season;
public class NewsManager
{
// SqlSessionFactory应该是应用级别
private static SqlSessionFactory sqlSessionFactory;
public static void main(String[] args) throws IOException
{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 1. 创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2. 打开SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 保存消息
// saveNews(sqlSession);
// 更新消息
// updateNews(sqlSession);
// 删除消息
// deleteNews(sqlSession);
// 查询消息
selectNews(sqlSession);
}
public static void saveNews(SqlSession sqlSession)
{
News news = new News(null, "测试标题", "测试内容");
news.setHappenSeason(Season.SUMMER);
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
newsMapper.saveNews(news);
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
}
public static void updateNews(SqlSession sqlSession)
{
News news = new News(1, "修改的标题", "修改的内容");
news.setHappenSeason(Season.FALL);
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
newsMapper.updateNews(news);
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
}
public static void deleteNews(SqlSession sqlSession)
{
// SqlSession的insert、update、delete方法都只是去执行DML语句
// 因此这个三个方法并没有本质的区别:它们都只是调用PreparedStatement的executeUpdate()方法来执行DML语句。
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
newsMapper.deleteNews(2);
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
}
public static void selectNews(SqlSession sqlSession)
{
// 此处的NewsMapper只是一个接口
// MyBatis会使用JDK的动态代理为Mapper接口生成实现类
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
System.out.println(newsMapper.getClass());
List<?> list = newsMapper.findNews(-1);
list.forEach(e -> {
System.out.println(((News) e).getHappenSeason());
});
System.out.println(list);
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
/*结果如下
* class com.sun.proxy.$Proxy17
* DEBUG [main] org.itcheng.app.dao.NewsMapper.findNews ==> Preparing: select news_id id, news_title title, news_content content, happen_season happenSeason from news_inf where news_id > ?
* DEBUG [main] org.itcheng.app.dao.NewsMapper.findNews ==> Parameters: -1(Integer)
* DEBUG [main] org.itcheng.app.dao.NewsMapper.findNews <== Total: 3(结果集条目数)
* SUMMER
* SUMMER
* SUMMER
* [org.itcheng.app.domain.News@5536379e, org.itcheng.app.domain.News@277f7dd3, org.itcheng.app.domain.News@6c8bca63]
* */
}
}
主配置文件
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="org.itcheng.app.domain"/>
typeAliases>
<typeHandlers>
<typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler"
javaType="org.itcheng.app.domain.Season"
jdbcType="INTEGER"/>
typeHandlers>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="root" />
dataSource>
environment>
environments>
<mappers>
<mapper resource="org/itcheng/app/dao/NewsMapper.xml" />
mappers>
configuration>
映射文件
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.itcheng.app.dao.NewsMapper">
<insert id="saveNews">
insert into news_inf values (null, #{title}, #{content}, #{happenSeason})
insert>
<update id="updateNews">
update news_inf set news_title = #{title}, news_content = #{content},
happen_season=#{happenSeason}
where news_id=#{id}
update>
<delete id="deleteNews">
delete from news_inf where news_id = #{xyz}
delete>
<select id="findNews" resultType="news">
select news_id id, news_title title, news_content content,
happen_season happenSeason
from news_inf where news_id > #{id}
select>
mapper>
用户类
package org.itcheng.app.domain;
// 默认的别名是news
public class News
{
private Integer id;
private String title;
private String content;
// 它是一个枚举类型的属性(程序要求happenSeason保存枚举的序号值)
private Season happenSeason;//数据库 类型为integer
//添加set和get方法,有参无参构造器
}
自定义枚举类
package org.itcheng.app.domain;
public enum Season
{
SPRING, SUMMER, FALL, WINTER;
}
表结构
create table news_inf
(
news_id int primary key auto_increment,
news_title varchar(255),
news_content varchar(255),
happen_season integer
);
主类
package lee;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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.itcheng.app.dao.NewsMapper;
import org.itcheng.app.domain.News;
import org.itcheng.app.domain.Season;
public class NewsManager
{
// SqlSessionFactory应该是应用级别
private static SqlSessionFactory sqlSessionFactory;
public static void main(String[] args) throws IOException
{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 1. 创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 2. 打开SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 保存消息
// saveNews(sqlSession);
// 更新消息
updateNews(sqlSession);
// 查询消息
// selectNews(sqlSession);
}
public static void saveNews(SqlSession sqlSession)
{
News news = new News(null, "测试标题", "测试内容");
news.setHappenSeason(Season.SUMMER);
news.setRecordSeason(Season.FALL);
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
newsMapper.saveNews(news);
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
/*
* DEBUG [main] org.itcheng.app.dao.NewsMapper.saveNews ==> Preparing: insert into news_inf values (null, ?, ?, ?, ?)
* DEBUG [main] org.itcheng.app.dao.NewsMapper.saveNews ==> Parameters: 测试标题(String), 测试内容(String), 1(Integer), FALL(String)
* DEBUG [main] org.itcheng.app.dao.NewsMapper.saveNews <== Updates: 1
* */
}
public static void updateNews(SqlSession sqlSession)
{
News news = new News(1, "修改的标题3", "修改的内容");
news.setHappenSeason(Season.FALL);
news.setRecordSeason(Season.FALL);
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
newsMapper.updateNews(news);
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
/*
* DEBUG [main] org.itcheng.app.dao.NewsMapper.updateNews ==> Preparing: update news_inf set news_title = ?, news_content = ?, happen_season=?, record_season=? where news_id=?
* DEBUG [main] org.itcheng.app.dao.NewsMapper.updateNews ==> Parameters: 修改的标题3(String), 修改的内容(String), 2(Integer), FALL(String), 1(Integer)
* DEBUG [main] org.itcheng.app.dao.NewsMapper.updateNews <== Updates: 1
* */
}
public static void selectNews(SqlSession sqlSession)
{
// 此处的NewsMapper只是一个接口
// MyBatis会使用JDK的动态代理为Mapper接口生成实现类
NewsMapper newsMapper = sqlSession.getMapper(NewsMapper.class);
System.out.println(newsMapper.getClass());
List<?> list = newsMapper.findNews(-1);
list.forEach(e -> {
News news = (News) e;
System.out.println(news.getHappenSeason() + "-->"
+ news.getRecordSeason());
});
System.out.println(list);
// 4. 提交事务
sqlSession.commit();
// 5. 关闭资源
sqlSession.close();
/*
* class com.sun.proxy.$Proxy17
* DEBUG [main] org.itcheng.app.dao.NewsMapper.findNews ==> Preparing: select news_id id, news_title title, news_content content, happen_season happenSeason, record_season recordSeason from news_inf where news_id > ?
* DEBUG [main] org.itcheng.app.dao.NewsMapper.findNews ==> Parameters: -1(Integer)
* DEBUG [main] org.itcheng.app.dao.NewsMapper.findNews <== Total: 1
* SUMMER-->FALL
* [org.itcheng.app.domain.News@50eca7c6]
*
* */
}
}
映射文件
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.itcheng.app.dao.NewsMapper">
<insert id="saveNews">
insert into news_inf values (null, #{title}, #{content},
#{happenSeason, typeHandler=org.apache.ibatis.type.EnumOrdinalTypeHandler,
javaType=org.itcheng.app.domain.Season}, #{recordSeason})
insert>
<update id="updateNews">
update news_inf set news_title = #{title}, news_content = #{content},
happen_season=#{happenSeason, typeHandler=org.apache.ibatis.type.EnumOrdinalTypeHandler,
javaType=org.itcheng.app.domain.Season}, record_season=#{recordSeason}
where news_id=#{id}
update>
<select id="findNews" resultMap="newsMap">
select news_id id, news_title title, news_content content,
happen_season happenSeason, record_season recordSeason
from news_inf where news_id > #{id}
select>
<resultMap type="news" id="newsMap">
<result column="id" property="id"/>
<result column="happenSeason" property="happenSeason"
typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"
javaType="org.itcheng.app.domain.Season"/>
resultMap>
mapper>
用户类
package org.itcheng.app.domain;
// 默认的别名是news
public class News
{
private Integer id;
private String title;
private String content;
// 它是一个枚举类型的属性(程序要求happenSeason保存枚举的序号值)
private Season happenSeason;//数据库类型 varchar(255)
// 它是一个枚举类型的属性(程序要求recordSeason保存枚举的名称)
private Season recordSeason;//数据库类型 varchar(255)
//添加set和get方法,有参无参构造器
}
自定义枚举类
package org.itcheng.app.domain;
public enum Season
{
SPRING, SUMMER, FALL, WINTER;
}
表结构
create table news_inf
(
news_id int primary key auto_increment,
news_title varchar(255),
news_content varchar(255),
happen_season varchar(255),
record_season varchar(255)
);