发表于: 2017-09-06 23:15:59

1 889


一、今日完成的任务

Q17-22

1、编写DAO,JdbcTemplate

1)实体类

public class Book {

private Integer id;

private String name;

private Double price;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Double getPrice() {

return price;

}

public void setPrice(Double price) {

this.price = price;

}

@Override

public String toString() {

return "Book [id=" + id + ", name=" + name + ", price=" + price + "]";

}

}

2)DAO类----接口

public void add(Book book) {

String sql="insert into book values (null,?,?)";

this.getJdbcTemplate().update(sql, book.getName(),book.getPrice());

}

public void delete(Book book) {

String sql="delete form book where id=?";

this.getJdbcTemplate().update(sql, book.getId());

}

public List<Book> findAll() {

String sql="SELECT * FROM book";

return this.getJdbcTemplate().query(sql, new BookRowMapper());

}

public void update(Book book) {

String sql="update book set name=? where id=?";

this.getJdbcTemplate().update(sql, book.getName(),book.getId());

}

class BookRowMapper implements RowMapper<Book>{

public Book mapRow(ResultSet rs, int rowNum) throws SQLException {

// TODO Auto-generated method stub

Book book=new Book();

book.setId(rs.getInt("id"));

book.setName(rs.getString("name"));

book.setPrice(rs.getDouble("price"));

return book;

}

}


3)spring的配置文件



<?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"

       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.xsd">

<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 配置c3p0连接池 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="${jdbc.driver}"/>

<property name="jdbcUrl" value="${jdbc.url}"/>

<property name="user" value="${jdbc.user}"/>

<property name="password" value="${jdbc.password}"/>

</bean>

<!-- 定义jdbctemplate -->

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

<property name="dataSource" ref="dataSource"/>

</bean>

<bean id="userDao" class="Test.UserDao">

<property name="jdbcTemplate" ref="jdbcTemplate"/>

</bean>

</beans>



注:jdbc.properties配置文件如下


jdbc.driver = com.mysql.jdbc.Driver

jdbc.url = jdbc:mysql:///itschool

jdbc.user = root

jdbc.password =root



5)测试类

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class TestDemo {

@Autowired

@Qualifier("bookDao")

private BookDao bookDao;

@Test

public void testadd(){

Book book=new Book();

book.setName("java开发经典");

book.setPrice(30.30);

}

}

测试结果如下




在测试过和遇见的问题如下:

1、failed to load applicationcontext  通过各项检查,得知是是classpath的路径写错了


今天心得是:重新复习

Spring JdbcTemplate 使用

明天任务:

Mybatis连接数据库的操作。





返回列表 返回列表
评论

    分享到