Spring+Mybatis开发示例

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

1,实现Spring+Mybatis+数据源的配置

2,实现枚举到数据库TINYINT类型的转换

3,slf4j日志配置方法

4,数据库增+删+改+查操作

5,实现效果界面+项目配置目录树

Spring+Mybatis开发示例_第1张图片     Spring+Mybatis开发示例_第2张图片

 

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 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 simpleList = null;
        simpleList = simpleService.query();
        model.addAttribute("simples", simpleList);
        return "simple";
    }
}

b)Mybatis配置文件

 




    
        
        
    
    
    
        
        
    

    
        
    


c)Mybatis映射mapper文件

 





    
        
        
        
        
        
        
    

    
        
    

    
        
    

    

    
        
        
            and id=#{id}
        
    

 

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配置文件




    
    
        
        
        
        
        
        
        
        
        
        
        
    

    
        
        
        
        
    

    
        
        
        
    

    
        
        
    


7,全部代码免费下载地址:http://download.csdn.net/detail/love254443233/7643049

 

 

 

你可能感兴趣的:(Java,基础)