spring使用JdbcTemplate和jdbcDaosupport及具名参数使用
关于jdbctemplate: 个人感觉比Java链接mysql那一套方便好维护多了,只需在配置文件维护即可 需要的包: com.springsource.net.sf.cglib-2.2.0.jar 具体步骤: 配置外部资源文件(db.properties) 配置mysal数据源 配置jdbctemplate bean.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd"> <!-- 装配自导扫描的包 --> context:component-scan base-package="com.spring.bean.jdbc"></context:component-scan 加载外部资源文件 context:property-placeholder location="classpath:db.properties"/> 配置MySQL数据源 bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> property name="driverClassName" value="${db.driverClassName}"property="url"="${db.url}"="username"="${db.username}"="password"="${db.password}"> </bean配置jdbcTemplate模板 ="jdbcTemplate"="org.springframework.jdbc.core.JdbcTemplate"="dataSource"> ref /> 配置namedParameterJdbcTemplate ,具名参数 ="namedParameterJdbcTemplate"="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"constructor-arg refconstructor-arg> beans> 外部资源文件 db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/students db.username=root db.password=root 实体students package com.spring.bean.jdbc; /** * 学生实体类 * @author Administrator * */ public class Students { private Integer Id; String name; String sex; private int age; String tel; public Integer getId() { return Id; } void setId(Integer id) { Id = id; } String getName() { name; } setName(String name) { this.name = String getSex() { sex; } setSex(String sex) { this.sex = getAge() { age; } void setAge( age) { this.age = String getTel() { tel; } setTel(String tel) { this.tel = tel; } @Override String toString() { return "Students [Id=" + Id + ",name=" + name + ",sex=" + sex + ",age=" + age + ",tel=" + tel + "]"; } } 实体course Course { Integer id; String coursename; String coursenameid; setId(Integer id) { this.id = String getCoursename() { coursename; } setCoursename(String coursename) { this.coursename = String getCoursenameid() { coursenameid; } setCoursenameid(String coursenameid) { this.coursenameid = coursenameid; } @Override return "Course [id=" + id + ",coursename=" + coursename + ",coursenameid=" + coursenameid + "]"; } } jdbctemplate import org.springframework.beans.factory.annotation.Autowired; org.springframework.jdbc.core.BeanPropertyRowMapper; org.springframework.jdbc.core.JdbcTemplate; org.springframework.jdbc.core.RowMapper; org.springframework.stereotype.Repository; @Repository StudentsDao { @Autowired JdbcTemplate jdbcTemplate; Students getStudent(String sql,Integer id) { RowMapper<Students> rowMapper = new BeanPropertyRowMapper<Students>(Students.); Students students = jdbcTemplate.queryForObject(sql,rowMapper,id); students; } } jdbcDaosupport javax.sql.DataSource; org.springframework.jdbc.core.support.JdbcDaoSupport; class CourseDao extends JdbcDaoSupport { 此处必须加入dataSource或jdbcTemplate要么报错如下 * Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required * 如不加jdbcTemplate。要用dataSource * name只能重新加入dataSource,为什么我用了setDataSource22为DataSource赋值 * 因在JdbcDaoSupport类中为final关键字修饰,不可重写 * @param dataSource */ @Autowired setDataSource22(DataSource dataSource) { setDataSource(dataSource); } * 获取课程 * sql * id * @return */ public Course getCourse(String sql, id){ RowMapper<Course> rowMapper = new BeanPropertyRowMapper<Course>(Course.); Course course = getJdbcTemplate().queryForObject(sql,1)"> course; } } 测试类具体见注释 java.util.ArrayList; java.util.HashMap; java.util.List; java.util.Map; org.junit.Test; org.springframework.context.ApplicationContext; org.springframework.context.support.ClassPathXmlApplicationContext; org.springframework.dao.DataAccessException; org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; org.springframework.jdbc.core.namedparam.MapSqlParameterSource; org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; org.springframework.jdbc.core.namedparam.SqlParameterSource; MainTest { private ApplicationContext ctx=null; StudentsDao studentsDao; JdbcTemplate jdbcTemplate; NamedParameterJdbcTemplate namedParameterJdbcTemplate; CourseDao courseDao; { ctx=new ClassPathXmlApplicationContext("bean.xml"); jdbcTemplate=(JdbcTemplate) ctx.getBean("jdbcTemplate"); studentsDao=(StudentsDao) ctx.getBean("studentsDao"); namedParameterJdbcTemplate=(NamedParameterJdbcTemplate)ctx.getBean("namedParameterJdbcTemplate"); courseDao=(CourseDao) ctx.getBean("courseDao"); } ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |