定义枚举:
public static enum AppStateEnum { Valid("有效"), Virtual("虚拟"), Hide("隐藏"); public String toString() { return this.appState; } private String appState; private AppStateEnum(String str) { this.appState = str; } }
实体:
public class SysApp { public int getAppId() { return appId; } public void setAppId(int appId) { this.appId = appId; } public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName; } public String getAppCode() { return appCode; } public void setAppCode(String appCode) { this.appCode = appCode; } public int getSorting() { return sorting; } public void setSorting(int sorting) { sorting = sorting; } public SysAppConstant.AppStateEnum getAppState() { return appState; } public void setAppState(SysAppConstant.AppStateEnum appState) { this.appState = appState; } private int appId; private String appName; private String appCode; private int sorting; private SysAppConstant.AppStateEnum appState; }
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.mamaguwen.dao.SysAppDao" > <resultMap id="BaseResultMap" type="com.mamaguwen.entity.SysApp" > <id column="AppId" property="appId" jdbcType="BIGINT" /> <result column="AppName" property="appName" jdbcType="VARCHAR" /> <result column="AppCode" property="appCode" jdbcType="VARCHAR" /> <result column="Sorting" property="sorting" jdbcType="VARCHAR" /> <result column="AppState" property="appState" jdbcType="VARCHAR" /> </resultMap> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" > Select AppId,AppName,AppCode,Sorting,AppState from sys_app Where AppId=#{appId} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" > delete from sys_app where AppId = #{appId} </delete> <insert id="insert" parameterType="com.mamaguwen.entity.SysApp" useGeneratedKeys="true" keyProperty="appId"> insert into sys_app (AppName,AppCode,Sorting,AppState) values (#{appName},#{appCode},#{sorting},#{appState}) </insert> <update id="updateSorting" parameterType="com.mamaguwen.entity.SysApp" > update sys_app set appstate=#{appState} where appId = #{appId} </update> </mapper>
测试:
@RunWith(value = SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring.xml", "classpath:spring-mybatis.xml" }) public class TestSysAppDao { private static final Logger logger = Logger.getLogger(Test_SysUser.class); private SysAppDao appDao; public SysAppDao getAppDao() { return appDao; } @Autowired public void setAppDao(SysAppDao appDao) { this.appDao = appDao; } @Test public void insert() { SysApp model=new SysApp(); model.setAppCode("001"); model.setAppName("王坤"); model.setAppState(AppStateEnum.Valid); appDao.insert(model); System.out.println(String.format("appId:%s", model.getAppId())); } @Test public void selectByPrimaryKey() { SysApp model= appDao.selectByPrimaryKey(1); System.out.println("调整状态:"+model.getAppState()); } }
需要注意的是 定义枚举时候,定义一个toString方法则 :
比如
AppStateEnum.Valid 存如数据库为 Valid读取出来在页面显示则可以显示成:
有效
时间: 2024-10-07 07:59:01