• 通用dao的demo


     

     
     
     

    代码片段

    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

  • 相关阅读:
    java获取Mp3播放时长
    angular ajax的使用及controller与service分层
    mysql数据库不能远程访问的问题
    linux安装ant
    jquery中,使用append增加元素时,该元素的绑定监听事件失效
    类变量与实例变量
    ajax两张传输数据方式
    jquery的.html(),.text()和.val()方法
    如何在sublime text上快速访问html页面?
    java学习笔记之线程2wait和notifyAll
  • 原文地址:https://www.cnblogs.com/u0mo5/p/4147984.html
Copyright © 2020-2023  润新知