通用dao的demo

简介:           代码片段 1. [代码]整型映射工具      ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package org.

 

 
 
 
 

代码片段

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

目录
相关文章
|
7月前
|
Java
javaEE架构必须懂的知识Base类
javaEE架构必须懂的知识Base类
30 0
|
6月前
|
Java
java工具类调用service层,mapper层
java工具类调用service层,mapper层
133 1
|
6月前
|
SQL Java 数据库连接
SSMP整合案例第二步 数据层dao开发及实现特殊查询
SSMP整合案例第二步 数据层dao开发及实现特殊查询
29 0
|
7月前
|
Java 数据库连接 mybatis
mybatis简单案例源码详细【注释全面】——Dao层接口(UserMapper.java)
mybatis简单案例源码详细【注释全面】——Dao层接口(UserMapper.java)
|
SQL 前端开发 JavaScript
JAVA框架的层级和各层理解 controller,service,dao,mapper,model等层和各包的作用
JAVA框架的层级和各层理解 controller,service,dao,mapper,model等层和各包的作用
994 0
|
开发框架 Java 数据库
JavaWeb - PO、BO、VO、DTO、POJO、DAO 概念 & 作用(附:转换图)
JavaWeb - PO、BO、VO、DTO、POJO、DAO 概念 & 作用(附:转换图)
545 0
JavaWeb - PO、BO、VO、DTO、POJO、DAO 概念 & 作用(附:转换图)
|
C++
c++ demo 05 类
c++ demo 05 类
79 0
|
Java 数据库
【Java SE 代码回顾】——day07_交互层、业务层、DAO层的商品管理Demo
【Java SE 代码回顾】——day07_交互层、业务层、DAO层的商品管理Demo
|
Java 关系型数据库 数据库连接
如何使用通用Mapper
集成方法请看上面的文档,集成后,可以继续阅读本页文档。 1. 继承通用的Mapper,必须指定泛型 例如下面的例子: public interface UserInfoMapper extends Mapper { //其他必须手写的接口... } 一旦继承了Mapper,继承的Mapper就拥有了Mapper所有的通用方法。
1958 0
|
设计模式 druid 数据库
DAO模式小结
DAO模式小结
151 0