首页 > 代码库 > Spring+Mybatis开发示例

Spring+Mybatis开发示例

写下来留个纪念(^~^)大神可飘过

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 = http://www.mamicode.com/"name", required = true) String name,>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=http://www.mamicode.com/"true"/>>
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=http://www.mamicode.com/"false"/>>
7,全部代码免费下载地址:http://download.csdn.net/detail/love254443233/7643049