Spring4-通过Maven构建Spring+JDBC
1.创建Maven项目,项目名(springdemo13),操作如图所示
成都创新互联公司专业为企业提供钟楼网站建设、钟楼做网站、钟楼网站设计、钟楼网站制作等企业网站建设、网页设计与制作、钟楼企业网站模板建站服务,10年钟楼做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
2.配置Maven,修改pom.xml文件,内容如下
1.0.0 shequ springdemo13 0.0.1-SNAPSHOT 1.7 UTF-8 UTF-8 codelds https://code.lds.org/nexus/content/groups/main-repo junit junit 4.10 org.springframework spring-core 4.1.4.RELEASE org.springframework spring-context 4.1.4.RELEASE org.springframework spring-jdbc 4.1.4.RELEASE MySQL mysql-connector-java 5.1.34
3.在src/main/java下创建实体Bean Forum,包名(com.mycompany.shequ.bean),如图所示
4.实体Bean Forum的内容如下
package com.mycompany.shequ.bean; public class Forum { private int fid; private String name; 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; } }
5.在src/main/java下创建接口ForumDao,包名(com.mycompany.shequ.dao),如图所示
6.接口ForumDao的内容如下
package com.mycompany.shequ.dao; import java.util.List; import com.mycompany.shequ.bean.Forum; public interface ForumDao { public Listquery() throws Exception ; }
7.在src/main/java下创建接口ForumDao的实现类ForumDaoImpl,包名(com.mycompany.shequ.dao.impl),如图所示
8.接口ForumDao的实现类ForumDaoImpl的内容如下
package com.mycompany.shequ.dao.impl; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; import com.mycompany.shequ.bean.Forum; import com.mycompany.shequ.dao.ForumDao; public class ForumDaoImpl implements ForumDao{ private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public Listquery() throws Exception { Connection conn = dataSource.getConnection(); String sql = "select * from hnsq_forum"; Statement smt = conn.createStatement(); ResultSet rs = smt.executeQuery(sql); List forumList = new ArrayList (); while(rs.next()){ Forum forum = new Forum(); forum.setFid(rs.getInt("fid")); forum.setName(rs.getString("name")); forumList.add(forum); } return forumList; } }
9.在src/main/resources下创建database目录,存储spring数据源spring-datasource.xml文件,如图所示
10.数据源spring-databsource.xml文件的内容如下
11.在src/main/resources下创建dao目录,存储相应的bean文件,文件名称spring-forum.xml,如图所示
12.spring-forum.xml的内容如下
13.在src/main/resources下创建spring核心配置文件spring-module.xml,如图所示
14.spring核心配置文件spring-module.xml的内容如下
15.在src/test/java目录下创建测试类ForumDaoImplTest,包名(com.mycompany.shequ.dao.impl),如图所示
16.测试类ForumDaoImplTest的内容如下
package com.mycompany.shequ.dao.impl; import java.util.List; 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 ForumDaoImplTest { @Test public void queryTest(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-module.xml"); ForumDao forumDao = (ForumDao) context.getBean("forumDao"); try { ListforumList = forumDao.queryDepartment(); for (Forum forum : forumList) { System.out.println(forum.getFid()); System.out.println(forum.getName()); } } catch (Exception e) { e.printStackTrace(); } } }
17.在测试类ForumDaoImplTest的queryTest方法上右键执行,输出结果如图所示
名称栏目:Spring4-通过Maven构建Spring+JDBC
网站路径:http://ybzwz.com/article/pcphsp.html