Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring对JDBC的支持 #7

Open
zhaozhao15 opened this issue May 11, 2019 · 0 comments
Open

Spring对JDBC的支持 #7

zhaozhao15 opened this issue May 11, 2019 · 0 comments

Comments

@zhaozhao15
Copy link
Owner

Spring对JDBC的支持


Spring中有jdbcTemplate支持,可以连接数据库。
官方文档:https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/#jdbc-JdbcTemplate
src中需要一个属性配置文件jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_spring
jdbc.username=root
jdbc.password=123456

配置文件中

<!--详细内容看官方文档-->
    <!--配置数据源-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!--加载指定路径的property文件-->
	<context:property-placeholder location="jdbc.properties"/>
    
    <!--定义jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    	<!--注入数据源属性-->
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
public class StudentDaoImpl implements StudentDao{
	private JdbcTemplate jdbcTemplate;
	
	//通过set方法把jdbcTemplate从配置文件里面注入进去
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int addStudent(Student student) {
		//sql当然还是照写
		String sql="insert into t_student values(null,?,?)";
		//对象数组
		Object []params=new Object[]{student.getName(),student.getAge()};
		//调用jdbc
		return jdbcTemplate.update(sql,params);
	}

	@Override
	public int updateStudent(Student student) {
		String sql="update t_student set name=?,age=? where id=?";
		Object []params=new Object[]{student.getName(),student.getAge(),student.getId()};
		return jdbcTemplate.update(sql,params);
	}

	@Override
	public int deleteStudent(int id) {
		String sql="delete from t_student where id=?";
		Object []params=new Object[]{id};
		return jdbcTemplate.update(sql,params);
	}

	@Override
	public List<Student> findStudents() {
		String sql="select * from t_student";
		final List<Student> studentList=new ArrayList<Student>();
		jdbcTemplate.query(sql, new RowCallbackHandler(){

			@Override
			public void processRow(ResultSet rs) throws SQLException {
				Student student=new Student();
				student.setId(rs.getInt("id"));
				student.setName(rs.getString("name"));
				student.setAge(rs.getInt("age"));
				studentList.add(student);
			}
			
		});
		return studentList;
	}

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant