代码片段

1. [代码]整型映射工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package org.dave.common.database.convert;
 
import java.sql.ResultSet;
import java.sql.SQLException;
 
/**
 * 整型映射工具
 * @author David Day
 */
public class IntegerConverter implements ResultConverter<Integer> {
 
    @Override
    public Integer convert(ResultSet rs) throws SQLException {
        return rs.getInt(1);
    }
 
}

2. [代码]长整型映射工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package org.dave.common.database.convert;
 
import java.sql.ResultSet;
import java.sql.SQLException;
 
/**
 * 长整型映射工具
 * @author 戴威
 */
public class LongConverter implements ResultConverter<Long> {
 
    @Override
    public Long convert(ResultSet rs) throws SQLException {
        return rs.getLong(1);
    }
 
}

3. [代码]数据模型的demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package org.dave.demo.model;
 
import org.dave.common.database.DataModel;
 
@SuppressWarnings("serial")
public class Demo extends DataModel {
     
    private Long id;
     
    private String name;
     
    private String pass;
     
    private String email;
 
    public Demo() {
        super();
    }
 
    public Demo(Long id) {
        super();
        this.id = id;
    }
 
    public Long getId() {
        return id;
    }
 
    public void setId(Long id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getPass() {
        return pass;
    }
 
    public void setPass(String pass) {
        this.pass = pass;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
}

4. [代码]demo数据模型转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package org.dave.demo.convert;
 
import java.sql.ResultSet;
import java.sql.SQLException;
 
import org.dave.common.database.convert.ResultConverter;
import org.dave.demo.model.Demo;
 
public class DemoConverter implements ResultConverter<Demo> {
 
    @Override
    public Demo convert(ResultSet rs) throws SQLException {
        Demo demo = new Demo(rs.getLong(1));
        demo.setName(rs.getString(2));
        demo.setPass(rs.getString(3));
        demo.setEmail(rs.getString(4));
        return demo;
    }
 
}

5. [代码]dao的demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.dave.demo.dao;
 
import java.sql.Connection;
import java.util.List;
 
import org.dave.common.database.access.DataAccess;
import org.dave.common.database.convert.IntegerConverter;
import org.dave.common.database.convert.LongConverter;
import org.dave.demo.convert.DemoConverter;
import org.dave.demo.model.Demo;
 
public class DemoDao extends DataAccess {
     
    public DemoDao(Connection conn) {
        super(conn);
    }
     
    /**
     * 增加
     * @param name
     * @param pass
     * @param email
     * @return
     */
    public Long add(String name, String pass, String email) {
        return super.insert("INSERT INTO demo(name,pass,email) values(?,?,?)",
                new LongConverter(), name, pass, email);
    }
     
    /**
     * 修改
     * @param id
     * @param name
     * @param pass
     * @param email
     */
    public void update(Long id, String name, String pass, String email) {
        super.update("UPDATE demo SET name=?,pass=?,email=? WHERE id=?",
                name, pass, email, id);
    }
     
    /**
     * 删除
     * @param id
     */
    public void delete(Long id) {
        super.update("DELETE FROM demo WHERE id=?", id);
    }
     
    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Demo findById(Long id) {
        return super.queryForObject("SELECT id,name,pass,email FROM demo WHERE id=?",
                new DemoConverter(), id);
    }
     
    /**
     * 查询所有
     * @return
     */
    public List<Demo> findAll() {
        return super.queryForList("SELECT id,name,pass,email FROM demo", new DemoConverter());
    }
     
    /**
     * 查询总记录数
     * @return
     */
    public Integer getCount() {
        return super.queryForObject("SELECT COUNT(*) FROM demo", new IntegerConverter());
    }
     
    /**
     * 分页查询
     * @param start
     * @param size
     * @return
     */
    public List<Demo> findByRange(int start, int size) {
        return super.queryForList("SELECT id,name,pass,email FROM demo limit ?,?",
                new DemoConverter(), start, size);
    }
 
}

6. [代码]基础service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package org.dave.common;
 
import java.sql.Connection;
 
import org.dave.common.database.DatabaseTransaction;
 
public abstract class BaseService {
     
    private DatabaseTransaction trans;
     
    protected BaseService(DatabaseTransaction trans) {
        this.trans = trans;
    }
     
    protected BaseService() { }
 
    protected DatabaseTransaction getTransaction() {
        return trans;
    }
     
    protected Connection getConnection() {
        return trans.getConnection();
    }
 
}

7. [代码]service的demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package org.dave.demo.service;
 
import java.util.List;
 
import org.dave.common.BaseService;
import org.dave.common.data.Pager;
import org.dave.common.database.DatabaseTransaction;
import org.dave.demo.dao.DemoDao;
import org.dave.demo.model.Demo;
 
public class DemoService extends BaseService {
 
    public DemoService(DatabaseTransaction trans) {
        super(trans);
    }
 
    public DemoService() {
        super();
    }
     
    /**
     * 增加
     * @param demo
     * @return
     */
    public Demo add(Demo demo) {
        DemoDao dao = new DemoDao(getConnection());
        Long id = dao.add(demo.getName(), demo.getPass(), demo.getEmail());
        return dao.findById(id);
    }
     
    /**
     * 更新
     * @param demo
     */
    public void update(Demo demo) {
        new DemoDao(getConnection()).update(demo.getId(), demo.getName(), demo.getPass(), demo.getEmail());
    }
     
    /**
     * 删除
     * @param id
     */
    public void delete(Long id) {
        new DemoDao(getConnection()).delete(id);
    }
     
    /**
     * 通过id查询
     * @param id
     * @return
     */
    public Demo findById(Long id) {
        return new DemoDao(getConnection()).findById(id);
    }
     
    /**
     * 查询所有
     * @return
     */
    public List<Demo> findAll() {
        return new DemoDao(getConnection()).findAll();
    }
     
    /**
     * 分页查询
     * @param page
     * @return
     */
    public List<Demo> findByPage(Pager page) {
        DemoDao dao = new DemoDao(getConnection());
        page.setTotalResults(dao.getCount());
        return dao.findByRange(page.getStartResults(), page.getPageSize());
    }
     
}

8. [代码]action的demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.dave.demo.action;
 
import java.util.List;
 
import org.dave.common.data.Pager;
import org.dave.common.database.DatabaseTransaction;
import org.dave.demo.model.Demo;
import org.dave.demo.service.DemoService;
 
public class DemoAction {
     
    public String add() {
        DatabaseTransaction trans = new DatabaseTransaction(true);
        try {
            new DemoService(trans).add(demo);
            trans.commit();
        } catch (Exception e) {
            trans.rollback();
            return "error";
        } finally {
            trans.close();
        }
        return "toList";
    }
     
    public String update() {
        DatabaseTransaction trans = new DatabaseTransaction(true);
        try {
            new DemoService(trans).update(demo);
            trans.commit();
        } catch (Exception e) {
            trans.rollback();
            return "error";
        } finally {
            trans.close();
        }
        return "toList";
    }
     
    public String info() {
        DatabaseTransaction trans = new DatabaseTransaction(true);
        try {
            demo = new DemoService(trans).findById(demo.getId());
        } catch (Exception e) {
            return "error";
        } finally {
            trans.close();
        }
        return "info";
    }
     
    public String findByPage() {
        DatabaseTransaction trans = new DatabaseTransaction(true);
        try {
            demoList = new DemoService(trans).findByPage(page);
        } catch (Exception e) {
            return "error";
        } finally {
            trans.close();
        }
        return "page";
    }
     
    public String findAll() {
        DatabaseTransaction trans = new DatabaseTransaction(true);
        try {
            demoList = new DemoService(trans).findAll();
        } catch (Exception e) {
            return "error";
        } finally {
            trans.close();
        }
        return "list";
    }
     
    private Pager page;
     
    private Demo demo;
     
    private List<Demo> demoList;
 
    public Pager getPage() {
        return page;
    }
 
    public void setPage(Pager page) {
        this.page = page;
    }
 
    public Demo getDemo() {
        return demo;
    }
 
    public void setDemo(Demo demo) {
        this.demo = demo;
    }
 
    public List<Demo> getDemoList() {
        return demoList;
    }
 
}

9. [文件] 源码和试例包 ~ 13KB     下载(556)

http://www.oschina.net/code/snippet_86510_4466

通用dao的demo的更多相关文章

  1. Java学习笔记之使用反射+泛型构建通用DAO

    PS:最近简单的学了学后台Servlet+JSP.也就只能学到这里了.没那么多精力去学SSH了,毕竟Android还有很多东西都没学完.. 学习内容: 1.如何使用反射+泛型构建通用DAO. 1.使用 ...

  2. 使用mybatis完成通用dao和通用service

    使用mybatis完成通用dao和通用service 概述: 使用通用dao和通用service可以减少代码的开发.可以将常用的增删改查放到通用dao中.对不同的or框架,基本上都有自己的实现如Spr ...

  3. Java反射结合JDBC写的一个通用DAO

    以前写反射只是用在了与设计模式的结合上,并没有考虑到反射可以与DAO结合.也是一个偶然的机会,被正在上培训的老师点到这个问题,才考虑到这个可能性,于是上网参考各种代码,然后自己动手开发了一个通用DAO ...

  4. Spring Boot-------JPA——EntityManager构建通用DAO

    EntityManager EntityManager 是用来对实体Bean 进行操作的辅助类.他可以用来产生/删除持久化的实体Bean,通过主键查找实体bean,也可以通过EJB3 QL 语言查找满 ...

  5. spring基于通用Dao的多数据源配置详解【ds1】

    spring基于通用Dao的多数据源配置详解 有时候在一个项目中会连接多个数据库,需要在spring中配置多个数据源,最近就遇到了这个问题,由于我的项目之前是基于通用Dao的,配置的时候问题不断,这种 ...

  6. Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x

    Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x ============================== 蕃薯耀 2018年 ...

  7. 十六、springboot整合Spring-data-jpa(二)之通用DAO接口与添加自定义方法

    @NoRepositoryBean:Spring Data Jpa在启动时就不会去实例化BaseRepository这个接口 1.通用接口: import org.springframework.da ...

  8. spring基于通用Dao的多数据源配置

    有时候在一个项目中会连接多个数据库,须要在spring中配置多个数据源,近期就遇到了这个问题,因为我的项目之前是基于通用Dao的,配置的时候问题不断.这样的方式和资源文件冲突:扫描映射文件的话,Sql ...

  9. Mybatis 使用PageHelper封装通用Dao分页方法

    参考: PageHelper官网:https://pagehelper.github.io/docs/howtouse/#3-%E5%A6%82%E4%BD%95%E5%9C%A8%E4%BB%A3% ...

随机推荐

  1. 杭电 2136 Largest prime factor(最大素数因子的位置)

    Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. 【摘录自MDN】预定义函数

    JavaScript语言有好些个顶级的内建函数: eval() eval()方法会对一串字符串形式的JavaScript代码字符求值. uneval()  uneval()方法创建的一个Object的 ...

  3. vue父孙组件传值($attr及$listeners)的使用

    父组件 <template> <div> <!-- 将值传给子组件 子组件可以获取自己想要的值 也可以忽视掉需要传给孙子组件的值 --> <!-- 同时获取通 ...

  4. C语言的变量存储方式和生存期

    2020.2.28日,封城一个多月了,紧邻毕业期,我在家抽空学习一下C. 看到了变量的存储方式和生存期这一章节,下面就是我整理的内容 下面是用于理解静态局部变量这个概念所写的代码,主要是需要分析一下函 ...

  5. 【转】python创建和删除文件

    #!/usr/bin/python #-*-coding:utf-8-*- #指定编码格式,python默认unicode编码 import os directory = "./dir&qu ...

  6. aws-ec2-upload

    EC2上建立文件夹并开放权限 $ mkdir /home/upload$ chmod 777 /home/upload 本机文件 上传 到EC2服务器$ scp -i awsub01.pem noip ...

  7. mysql8 Could not retrieve transation read-only status server

    想回顾下ssm,看着网上别的帖子手动搭了一个,一直报Could not retrieve transation read-only status server , java.sql.SQLExcept ...

  8. WAV格式解析

    WAV为微软公司(Microsoft)开发的一种声音文件格式,它符合RIFF(Resource Interchange File Format)文件规范,用于保存Windows平台的音频信息资源,被W ...

  9. 劫后余生--New Start

    被搁置的计划 It was the best of times,it was the worst of times,it was the age of wisidom,it was the age o ...

  10. 转:Entity Framework 5.0 Code First全面学习

    Code First 约定 借助 CodeFirst,可通过使用 C# 或Visual Basic .NET 类来描述模型.模型的基本形状可通过约定来检测.约定是规则集,用于在使用 Code Firs ...