写下来留个纪念(^~^)大神可飘过
1,实现Spring+Mybatis+数据源的配置
2,实现枚举到数据库TINYINT类型的转换
3,slf4j日志配置方法
4,数据库增+删+改+查操作
5,实现效果界面+项目配置目录树
6,关键代码:
a)控制器
package com.fresh.lyh.simple.controller; import com.fresh.lyh.simple.model.Simple; import com.fresh.lyh.simple.model.cenum.SimpleStatusEnum; import com.fresh.lyh.simple.service.SimpleService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.annotation.Resource; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; /** * 作者 yaohua.liu * 时间 2014-06-12 20:20 * 包名 com.qunar.qfound.controller * 说明 ... */ @Controller @RequestMapping("/") public class SimpleController { @Resource SimpleService simpleService; Logger logger = LoggerFactory.getLogger(SimpleController.class); private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); @RequestMapping("") public Object go(Model model) { logger.info("======================================>go.do"); return forwardToIndex(model); } @RequestMapping("regedit.do") public Object regedit(@RequestParam(value = "name", required = true) String name, @RequestParam(value = "passwd", required = true) String passwd, @RequestParam(value = "birth", required = false) String birth, @RequestParam(value = "status", required = false) int status, Model model) { logger.info("===============================regedit.do: " + name + ":" + passwd + ":" + birth); Simple simple = new Simple(name, passwd); simple.setCreateTime(new Date()); try { simple.setBirth(sdf.parse(birth)); simple.setStatus(SimpleStatusEnum.codeOf(status)); } catch (ParseException e) { logger.info("===============解析生日日期失败"); } if (simpleService == null) logger.info("simpleService注入失败!!"); simpleService.regedit(simple); return forwardToIndex(model); } @RequestMapping("update.do") public Object update(@RequestParam(value = "name", required = true) String name, @RequestParam(value = "id", required = true) int id, Model model) { logger.info("=============================== update.do:" + name + ":" + id); Simple simple = new Simple(); simple.setName(name); simple.setId(id); simpleService.update(simple); return forwardToIndex(model); } @RequestMapping("query.do") public Object query(@RequestParam(value = "name", required = false) String name, Model model) { logger.info("===============================query.do: " + name); Simple simple = new Simple(); simple.setName(name); List<Simple> simpleList = simpleService.query(simple); model.addAttribute("simples", simpleList); return "simple"; } @RequestMapping("delete.do") public Object delete(@RequestParam(value = "id", required = true) int id, Model model) { logger.info("===============================delete.do: " + id); Simple simple = new Simple(); simple.setId(id); simpleService.delete(simple); return forwardToIndex(model); } private String forwardToIndex(Model model) { List<Simple> simpleList = null; simpleList = simpleService.query(); model.addAttribute("simples", simpleList); return "simple"; } }b)Mybatis配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 自动把create_time 转换成 createTime 不需要在添加 as 语句,如: select create_time as createTime from your_table_name;//以前使用这样的语句 使用 select create_time from your_table_name;//现在使用这样的语句 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- 类的别名 以前使用:<typeAlias type="com.qunar.qfound.model.Simple" alias="Simple"/> 现在使用:<package name="com.qunar.qfound.model"/>可完成model下面使用实体类的扫描 后面方式默认把类的别名命名为类名且第一个字母为小写,也支持直接使用类名,如: 在mapper/simple.xml中可以使用simple也可以使用Simple --> <typeAliases> <package name="com.fresh.lyh.simple.model"/> <package name="com.fresh.lyh.simple.model.cenum"/> </typeAliases> <typeHandlers> <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="SimpleStatusEnum" /> </typeHandlers> </configuration>
c)Mybatis映射mapper文件
<?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.fresh.lyh.simple.dao.SimpleDAO"> <resultMap id="simpleResult" type="Simple"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="birth" property="birth"/> <result column="create_time" property="createTime"/> <result column="passwd" property="passwd"/> <result column="status" property="status" javaType="SimpleStatusEnum" jdbcType="TINYINT"/> </resultMap> <insert id="regedit" parameterType="Simple" useGeneratedKeys="true" keyProperty="id"> <![CDATA[ insert into simple( name, passwd, birth, status, create_time ) values ( #{name}, #{passwd}, #{birth}, #{status,javaType=SimpleStatusEnum,jdbcType=TINYINT}, #{createTime} ) ]]> </insert> <update id="update" parameterType="Simple"> <![CDATA[ update simple set name = #{name} where id=#{id} ]]> </update> <select id="query" parameterType="Simple" resultMap="simpleResult"> <![CDATA[ select id, name, passwd, birth, status, create_time from simple ]]> <where> <if test="name!=null and name!=''"> and name=#{name} </if> </where> </select> <delete id="delete" parameterType="java.util.HashMap"> <![CDATA[ delete from simple ]]> <where> and id=#{id} </where> </delete> </mapper>
d)枚举类文件
package com.fresh.lyh.simple.model.cenum; /** * 作者 yaohua.liu * 时间 2014-07-13 20:21 * 包名 com.qunar.qfound.cenum * 说明 ... */ public enum SimpleStatusEnum { normal(0, "正常"), delete(1, "已删除"); private Integer code; private String role; SimpleStatusEnum(Integer code, String role) { this.role = role; this.code = code; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public static SimpleStatusEnum codeOf(Integer code) { for (SimpleStatusEnum status : values()) { if (status.code == code) return status; } throw new IllegalArgumentException("Invalid role code: " + code); } }e)dao配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- =================================================== 数据源 --> <bean id="abstractPoolDatatSource" abstract="true" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> <property name="fairQueue" value="false"/> <property name="initialSize" value="1"/> <property name="maxActive" value="50"/> <property name="maxIdle" value="20"/> <property name="maxWait" value="5000"/> <property name="minIdle" value="1"/> <property name="removeAbandoned" value="true"/> <property name="removeAbandonedTimeout" value="30"/> <property name="testOnBorrow" value="true"/> <property name="validationQuery" value="select 1"/> <property name="validationInterval" value="500000"/> </bean> <bean id="dataSource" parent="abstractPoolDatatSource"> <property name="driverClassName" value="${mysql.jdbc.driver}"/> <property name="url" value="${mysql.jdbc.url}"/> <property name="username" value="${mysql.jdbc.username}"/> <property name="password" value="${mysql.jdbc.password}"/> </bean> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:mappers/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.fresh.lyh.simple.dao"/> <property name="sqlSessionFactoryBeanName" value="sessionFactory"/> </bean> </beans>
7,全部代码免费下载地址:http://download.csdn.net/detail/love254443233/7643049