1.创建项目,项目名称(springdemo3),如图所示
2.在项目中创建相应的目录存储文件(src->源码目录,test->测试目录,lib->jar包目录),如图所示
3.在lib目录中创建子目录,用于存储相应的jar包,如图所示
4.在lib目录的子目录添加相应的jar包,如图所示
5.在src中创建Forum Bean,包名(com.mycompany.shequ.bean),如图所示
6.Forum Bean中的内容如下
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; } }
7.在src下创建dao 接口ForumDAO,包名(com.mycompany.shequ.dao),如图所示
8.接口ForumDAO的内容如下
package com.mycompany.shequ.dao; import com.mycompany.shequ.bean.Forum; public interface ForumDAO { public void insert(Forum forum); public Forum findByForumId(int fid); }
9.在src中创建ForumDAO的实现类JdbcForumDAO,包名(com.mycompany.shequ.dao.impl),如图所示
10.JdbcForumDAO类中的内容如下
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; } }
11.在src下创建数据源配置文件Spring-Datasource.xml,如图所示
12.数据源配置文件Spring-Datasource.xml的内容如下
<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>
13.在src中创建Bean配置文件Spring-Forum.xml,如图所示
14.Bean配置文件Spring-Forum.xml的内容如下
<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>
15.在src中创建核心配置文件applicationContext.xml文件,如图所示
16.核心配置文件applicationContext.xml文件的内容如下
<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>
17.在test目录中创建测试类JdbcForumDAOTest,包名(com.mycompany.shequ.dao.impl),如图所示
18.测试类JdbcForumDAOTest的内容如下
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方法
时间: 2024-10-29 19:10:08