摘要:本文主要向大家介绍了MFC编程实例:从零实现MVC框架之实体映射(3),通过具体的内容向大家展示,希望对大家学习MFC编程实例有所帮助。
本文主要向大家介绍了MFC编程实例:从零实现MVC框架之实体映射(3),通过具体的内容向大家展示,希望对大家学习MFC编程实例有所帮助。
实体与表的映射,我们采用注解的方式实现;这一节的实现目标是把实体类生成相应的增删改查sql语句。
注解
实体与表映射注解
[java] view plain copy
1. package com.hc.annotation;
2.
3. import java.lang.annotation.ElementType;
4. import java.lang.annotation.Retention;
5. import java.lang.annotation.RetentionPolicy;
6. import java.lang.annotation.Target;
7. /**
8. *
9. * @author chuer
10. * @date 2014-7-16 上午9:33:18
11. * @version V1.0
12. */
13. @Retention(RetentionPolicy.RUNTIME)
14. @Target({ElementType.TYPE})
15. public @interface Entity {
16.
17. /**
18. * 实体名称
19. */
20. String entityName();
21.
22. /**
23. * 数据库表名称
24. */
25. String tableName() default "";
26.
27. }
类字段与表字段映射注解
[java] view plain copy
1. package com.hc.annotation;
2.
3. import java.lang.annotation.ElementType;
4. import java.lang.annotation.Retention;
5. import java.lang.annotation.RetentionPolicy;
6. import java.lang.annotation.Target;
7. /**
8. *
9. * @author chuer
10. * @date 2014-7-16 上午9:33:10
11. * @version V1.0
12. */
13. @Retention(RetentionPolicy.RUNTIME)
14. @Target({ElementType.FIELD})
15. public @interface Column {
16.
17. // 表中字段约束信息,(类型,长度,索引,是否为空,默认值)
18. Class<?> type(); // 字段类型
19. String name(); //数据库字段名
20. boolean isPrimary() default false; //是否主键
21. }
实体类
[java] view plain copy
1. package com.hc.sample.entity;
2.
3. import java.sql.SQLException;
4.
5. import com.hc.annotation.Column;
6. import com.hc.annotation.Entity;
7. /**
8. *
9. * @author chuer
10. * @date 2014-7-16 上午9:33:02
11. * @version V1.0
12. */
13. @Entity(entityName="UserEntity",tableName="user_m")
14. public class UserEntity{ //TODO extends AbstractEntity
15.
16. public UserEntity(){
17. super();
18. }
19.
20. @Column(type=long.class,name="id",isPrimary=true)
21. private long id;
22.
23. @Column(type=String.class,name="name")
24. private String name;
25.
26.
27. public long getId() {
28. return id;
29. }
30.
31. public void setId(long id) {
32. this.id = id;
33. }
34.
35. public String getName() {
36. return name;
37. }
38.
39.
40. public void setName(String name) throws SQLException {
41. this.name = name;
42. //更新数据到数据库 只有持久化的数据才可以更新操作
43. //TODO update("name");
44. }
45.
46.
47. }
数据表只有两个字段 id 和 name 。其中id是主键 name是 varchar 类型。
sql管理类
此类根据实体类生成相应的sql语句。并对一些sql进行缓存
[java] view plain copy
1. package com.hc.core;
2. /**
3. *
4. * @author chuer
5. * @date 2014-7-16 上午9:46:52
6. * @version V1.0
7. */
8. public interface ISql {
9.
10. public String getInsertSql(Class<?> t);
11.
12. public String getUpdateSql(Class<?> t,String column);
13.
14. public String getDeleteSql(Class<?> t);
15.
16. public String getQuerySql(Class<?> t);
17.
18. public String getSingleSql(Class<?> t);
19. }
实现类
[java] view plain copy
1. package com.hc.sql;
2.
3. import java.lang.reflect.Field;
4. import java.util.concurrent.ConcurrentHashMap;
5.
6. import com.hc.annotation.Column;
7. import com.hc.annotation.Entity;
8. import com.hc.core.ISql;
9. /**
10. *
11. * @author chuer
12. * @date 2014-7-16 上午9:38:47
13. * @version V1.0
14. */
15. public class SqlManager implements ISql{
16. private SqlManager(){}
17. private static SqlManager manager = new SqlManager();
18.
19. private static final ConcurrentHashMap<Class<?>,String> insertSqlMap = new ConcurrentHashMap<>(); //缓存对象插入sql语句
20. private static final ConcurrentHashMap<String,String> updateSqlMap = new ConcurrentHashMap<>(); //缓存对象更新sql语句
21. private static final ConcurrentHashMap<Class<?>,String> deleteSqlMap = new ConcurrentHashMap<>(); //缓存对象删除sql语句
22.
23. public static SqlManager getInstance(){
24. return manager;
25. }
26.
27. @Override
28. public String getInsertSql(Class<?> cls) {
29. String sql = insertSqlMap.get(cls);
30. if(sql!=null){
31. return sql;
32. }
33. StringBuilder sb = new StringBuilder("insert into ");
34. Entity entity = cls.getAnnotation(Entity.class);
35. sb.append(entity.tableName());
36. sb.append("(");
37.
38. Field[] fields = cls.getDeclaredFields();
39. for(Field filed : fields ){
40. if(filed.isAnnotationPresent(Column.class)){
41. Column column = filed.getAnnotation(Column.class);
42. sb.append(column.name()+",");
43. }
44. }
45. sb.deleteCharAt(sb.length()-1);
46. sb.append(") values (");
47. for(Field filed : fields){
48. if(filed.isAnnotationPresent(Column.class)){
49. sb.append("?,");
50. }
51. }
52. sb.deleteCharAt(sb.length()-1);
53. sb.append(")");
54. insertSqlMap.put(cls, sb.toString());
55. return sb.toString();
56. }
57.
58. @Override
59. public String getUpdateSql(Class<?> cls,String column) {
60. String sql = updateSqlMap.get(cls.toString()+column);
61. if(sql!=null){
62. return sql;
63. }
64.
65.
66. StringBuilder sb = new StringBuilder("update ");
67. Entity entity = cls.getAnnotation(Entity.class);
68. sb.append(entity.tableName());
69. sb.append(" set "+column+"=? where ");
70. Field[] fields = cls.getDeclaredFields();
71. for(Field field : fields){
72. if(field.isAnnotationPresent(Column.class)){
73. Column annotation = field.getAnnotation(Column.class);
74. if(annotation.isPrimary()){
75. sb.append(field.getName()+"=?");
76. break;
77. }
78. }
79. }
80. updateSqlMap.put(cls.toString()+column, sb.toString());
81. return sb.toString();
82. }
83.
84. @Override
85. public String getDeleteSql(Class<?> cls) {
86. String sql = deleteSqlMap.get(cls);
87. if(sql!=null){
88. return sql;
89. }
90.
91. StringBuilder sb = new StringBuilder("delete from ");
92. Entity entity = cls.getAnnotation(Entity.class);
93. sb.append(entity.tableName());
94. sb.append(" where ");
95. Field[] fields = cls.getDeclaredFields();
96. for(Field field : fields){
97. if(field.isAnnotationPresent(Column.class)){
98. Column annotation = field.getAnnotation(Column.class);
99. if(annotation.isPrimary()){
100. sb.append(field.getName()+"=?");
101. break;
102. }
103. }
104. }
105. deleteSqlMap.put(cls, sb.toString());
106. return sb.toString();
107. }
108.
109. @Override
110. public String getQuerySql(Class<?> cls) {
111. StringBuilder sb = new StringBuilder("select * from ");
112. Entity entity = cls.getAnnotation(Entity.class);
113. sb.append(entity.tableName()+" where 1=1 ");
114. return sb.toString();
115. }
116.
117.
118. @Override
119. public String getSingleSql(Class<?> cls) {
120. StringBuilder sb = new StringBuilder("select * from ");
121. Entity entity = cls.getAnnotation(Entity.class);
122. sb.append(entity.tableName()+" where 1=1 and ");
123.
124. Field[] fields = cls.getDeclaredFields();
125. for(Field field : fields){
126. if(field.isAnnotationPresent(Column.class)){
127. Column annotation = field.getAnnotation(Column.class);
128. if(annotation.isPrimary()){
129. sb.append(field.getName()+"=?");
130. break;
131. }
132. }
133. }
134. return sb.toString();
135. }
136.
137. }
测试类
[java] view plain copy
1. package com.hc.sample.entity;
2.
3. import com.hc.sql.SqlManager;
4.
5. public class TestEntity {
6.
7. public static void main(String[] args) {
8.
9. String insertSql = SqlManager.getInstance().getInsertSql(UserEntity.class);
10. System.out.println(insertSql);
11.
12. String deleteSql = SqlManager.getInstance().getDeleteSql(UserEntity.class);
13. System.out.println(deleteSql);
14.
15.
16. String querySql = SqlManager.getInstance().getQuerySql(UserEntity.class);
17. System.out.println(querySql);
18.
19.
20. String singleSql = SqlManager.getInstance().getSingleSql(UserEntity.class);
21. System.out.println(singleSql);
22.
23.
24. String updateSql = SqlManager.getInstance().getUpdateSql(UserEntity.class,"name");
25. System.out.println(updateSql);
26.
27.
28. }
29. }
运行结果如下:
[sql] view plain copy
1. insert into user_m(id,name) values (?,?)
2. delete from user_m where id=?
3. select * from user_m where 1=1
4. select * from user_m where 1=1 and id=?
5. update user_m set name=? where id=?
本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言VC/MFC频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号