标签:spring4-jdbc
package com.mycompany.shequ.bean; public class Forum { private int fid; private String name; private int displayorder; private int postcount; private int isshow; public Forum() { super(); } public Forum(int fid, String name, int displayorder, int postcount, int isshow) { this.fid = fid; this.name= name; this.displayorder = displayorder; this.postcount = postcount; this.isshow = isshow; } public int getFid() { return fid; } public void setFid(int fid) { this.fid = fid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getDisplayorder() { return displayorder; } public void setDisplayorder(int displayorder) { this.displayorder = displayorder; } public int getPostcount() { return postcount; } public void setPostcount(int postcount) { this.postcount = postcount; } public int getIsshow() { return isshow; } public void setIsshow(int isshow) { this.isshow = isshow; } }
package com.mycompany.shequ.dao; import com.mycompany.shequ.bean.Forum; public interface ForumDAO { public void insert(Forum forum); public Forum findByForumId(int fid); }
package com.mycompany.shequ.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; import com.mycompany.shequ.bean.Forum; import com.mycompany.shequ.dao.ForumDAO; public class JdbcForumDAO implements ForumDAO { private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } @Override public void insert(Forum forum) { String sql = "INSERT INTO hnsq_forum (name,displayorder,postcount,isshow) values(?,?,?,?)"; Connection conn = null; try { conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, forum.getName()); ps.setInt(2, forum.getDisplayorder()); ps.setInt(3,forum.getPostcount()); ps.setInt(4, forum.getIsshow()); ps.executeUpdate(); ps.close(); } catch (SQLException e) { throw new RuntimeException(e); }finally{ if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } @Override public Forum findByForumId(int fid) { String sql = "select * from hnsq_forum where fid = ?"; Connection conn = null; try { conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, fid); Forum forum = null; ResultSet rs = ps.executeQuery(); if(rs.next()){ forum = new Forum(rs.getInt("fid"),rs.getString("name"), rs.getInt("displayorder"),rs.getInt("postcount"), rs.getInt("isshow")); } rs.close(); ps.close(); return forum; } catch (SQLException e) { e.printStackTrace(); } return null; } }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/b_demo_two" /> <property name="username" value="root" /> <property name="password" value="" /> </bean> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="forumDao" class="com.mycompany.shequ.dao.impl.JdbcForumDAO"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <import resource="Spring-Datasource.xml" /> <import resource="Spring-Forum.xml" /> </beans>
package com.mycompany.shequ.dao.impl; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mycompany.shequ.bean.Forum; import com.mycompany.shequ.dao.ForumDAO; public class JdbcForumDAOTest { @Test public void testInsert(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ForumDAO forumDao = (ForumDAO) context.getBean("forumDao"); Forum forum = new Forum(); forum.setName("demo"); forum.setDisplayorder(0); forum.setIsshow(0); forum.setPostcount(0); forumDao.insert(forum); } @Test public void testFindByForumId(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ForumDAO forumDao = (ForumDAO) context.getBean("forumDao"); Forum forum = forumDao.findByForumId(34); System.out.println(forum.getName()); } }
19.测试testInsert方法
20.测试testFindByForumId方法
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1908855
标签:spring4-jdbc
原文地址:http://suyanzhu.blog.51cto.com/8050189/1908855