码云账号不再使用社区账号进行二次身份验证的通知 详情
私信发送成功
检测到您已登录技术前沿网,是否 一键登录码云
Watch Star Fork

技术前沿网 / spring-demoJava

利用模版方法和泛型重构dao层
messfairy 提交于   master
commit 5316cc9b10d902da191af7bef7d6fb88fbe23848
1 parent bd47e13b39

6 文件发生了变化, 影响行数: +174 -7

  • README.md +1 -1
  • src/main/java/messfairy/DatabaseConfig.java +6 -6
  • src/main/java/messfairy/controller/CommentsController.java +21 -0
  • src/main/java/messfairy/dao/AbstractDao.java +39 -0
  • src/main/java/messfairy/dao/CommentsDao.java +62 -0
  • src/main/java/messfairy/entity/Comment.java +45 -0
@@ -6,4 +6,4 @@
##2.pom.xml
###您只需要把依赖写入根目录下的pom.xml,并指定打包方式为war,然后push你的源码。点击mopass部署,部署过程中,mopass就会解析pom中的maven依赖并自动获取依赖,编译打包你的源码为war包,放到mopass的web服务器tomcat或jetty下。
##3.启动应用
-###这时你再点击运行或重新运行,mopass将为你启动web服务器,运行war包,数十秒后,你就可以访问mopass为你定制的服务地址啦:http://springblog.oschina.mopaas.com
\ No newline at end of file
+###这时你再点击运行或重新运行,mopass将为你启动web服务器,运行war包,数十秒后,你就可以访问mopass为你定制的服务地址啦:http://springblog.oschina.mopaas.com
@@ -20,12 +20,12 @@ public class DatabaseConfig {
// instantiate, configure and return DataSource
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
-// dataSource.setUrl("jdbc:mysql://localhost/myblog?characterEncoding=utf-8");
-// dataSource.setUsername("root");
-// dataSource.setPassword("root");
- dataSource.setUrl("jdbc:mysql://10.4.12.173:3306/dd984836e9dde4d35b01ab87ec11b14f1?characterEncoding=utf-8");
- dataSource.setUsername("umKFSjZ742Zih");
- dataSource.setPassword("pTF2665xEkjWY");
+ dataSource.setUrl("jdbc:mysql://localhost/myblog?characterEncoding=utf-8");
+ dataSource.setUsername("root");
+ dataSource.setPassword("root");
+// dataSource.setUrl("jdbc:mysql://10.4.12.173:3306/dd984836e9dde4d35b01ab87ec11b14f1?characterEncoding=utf-8");
+// dataSource.setUsername("umKFSjZ742Zih");
+// dataSource.setPassword("pTF2665xEkjWY");
dataSource.setValidationQuery("SELECT 1");
configureDataSource(dataSource);
return dataSource;
@@ -0,0 +1,21 @@
+package messfairy.controller;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+/**
+ * Created by hasee-pc on 2015/1/19.
+ */
+@Controller
+@RequestMapping("/posts")
+public class CommentsController {
+ public void save(){
+
+ }
+ public void create(){
+
+ }
+ public void get(final long id){
+
+ }
+}
@@ -0,0 +1,39 @@
+package messfairy.dao;
+
+import messfairy.entity.User;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataAccessException;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.RowMapper;
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
+
+import javax.sql.DataSource;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.List;
+
+/**
+ * Created by hasee-pc on 2015/1/19.
+ */
+public abstract class AbstractDao<T> {
+ protected RowMapper<T> mapper = mapper();
+ protected JdbcTemplate template;
+ protected NamedParameterJdbcTemplate jdbcTemplate;
+ protected RowMapper<T> mapper(){
+ return new RowMapper<T>() {
+ public T mapRow(final ResultSet rs, int rowNum) throws SQLException {
+ return map(rs);
+ }
+ };
+ }
+ public List<T> queryAll(final String abstractSql) throws DataAccessException {
+ return this.jdbcTemplate.query(abstractSql, mapper());
+ }
+ protected abstract T map(final ResultSet rs) throws SQLException;
+ @Autowired
+ public void setDataSource(DataSource dataSource){
+ this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
+ this.template = new JdbcTemplate(dataSource);
+ }
+
+}
@@ -0,0 +1,62 @@
+package messfairy.dao;
+
+import messfairy.entity.Comment;
+import org.springframework.dao.DataAccessException;
+import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
+import org.springframework.jdbc.core.namedparam.SqlParameterSource;
+import org.springframework.stereotype.Repository;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Repository
+public class CommentsDao extends AbstractDao<Comment>{
+ @Override
+ protected Comment map(final ResultSet rs) throws SQLException {
+ Comment comment = new Comment();
+ comment.setId(Integer.valueOf(rs.getInt("id")));
+ comment.setPid(rs.getInt("pid"));
+ comment.setContent(rs.getString("content"));
+ comment.setCreateDate(rs.getDate("create_date"));
+ return comment;
+ }
+ public void create(final Comment comment) {
+ String insertSql = "INSERT INTO COMMENTS(name, first_name, last_name, password, email, create_date) VALUES (?, ?, ?, ?, ?, ?)";
+ Object[] params = { comment.getContent(), new Date()};
+ int[] types = { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP };
+ int row = this.template.update(insertSql, params, types);
+ System.out.println(row + " row inserted.");
+ }
+
+ public Comment byId(int id) throws DataAccessException {
+ String sql = "select id, pid, content, create_date from COMMENTS where id=:id";
+ SqlParameterSource namedParameters = new MapSqlParameterSource("id", Integer.valueOf(id));
+ return this.jdbcTemplate.queryForObject(sql, namedParameters, mapper());
+ }
+
+ public Comment byName(final String name) throws DataAccessException{
+ String sql = "select id, name, password, create_date, email from USERS where name = :name";
+ SqlParameterSource namedParameters = new MapSqlParameterSource("name", name);
+ return this.jdbcTemplate.queryForObject(sql, namedParameters, mapper());
+ }
+
+ public Comment byLogin(final Comment comment) throws DataAccessException{
+ String sql = "select id, name, password, create_date from USERS where name = :name and content = :content";
+ Map<String, String> paramsMap = new HashMap<String, String>();
+ paramsMap.put("name", comment.getContent());
+ paramsMap.put("content", comment.getContent());
+ SqlParameterSource namedParameters = new MapSqlParameterSource(paramsMap);
+ return this.jdbcTemplate.queryForObject(sql, namedParameters, mapper);
+ }
+
+ public List<Comment> queryAll() throws DataAccessException{
+ String sql = "select id, name, password, create_date, email from USERS";
+ List<Comment> comments = this.jdbcTemplate.query(sql, mapper);
+ return comments;
+ }
+}
@@ -0,0 +1,45 @@
+package messfairy.entity;
+
+import java.util.Date;
+
+/**
+ * Created by hasee-pc on 2015/1/19.
+ */
+public class Comment {
+ private int id;
+ private int pid;
+ private String content;
+ private Date createDate;
+
+ public String getContent() {
+ return content;
+ }
+
+ public void setContent(String content) {
+ this.content = content;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public Date getCreateDate() {
+ return createDate;
+ }
+
+ public void setCreateDate(Date createDate) {
+ this.createDate = createDate;
+ }
+
+ public int getPid() {
+ return pid;
+ }
+
+ public void setPid(int pid) {
+ this.pid = pid;
+ }
+}

登录 后才可以发表评论

Float_left Icon_close