Spring JDBC
使用Spring框架访问数据库首先要导入节点
下面以MySQL为例
//事务 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.3.RELEASE</version> </dependency> //数据库驱动jar <dependency> <groupId>org.wisdom-framework</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34_1</version> </dependency> //Spring jdbc <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.3.RELEASE</version> </dependency>
2.在xml文件中配置dataSource
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql:///book"
p:username="root"
p:password="xxxxxxx"> </bean>
3.注入JdbcTemplate
<bean class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource"> </bean>
4.编写实体类
package book.entity; /** * Created by Administrator on 2017/3/30/030. */ public class Book { private Integer bId; private String bName; private double bPrice; public Integer getbId() { return bId; } public void setbId(Integer bId) { this.bId = bId; } public String getbName() { return bName; } public void setbName(String bName) { this.bName = bName; } public double getbPrice() { return bPrice; } }
5.编写dao层接口
package book.dao; import book.entity.Book; import java.util.List; /** * Created by Administrator on 2017/3/30/030. */ public interface IBookDao { int addBook(Book book); List<Book> bookList(); }
6.编写RowMapper类用于解析结果集生成泛型
package book.dao.impl; import book.entity.Book; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; import java.sql.SQLException; /** * Created by Administrator on 2017/3/30/030. */ public class BookRowMap implements RowMapper<Book> { @Override public Book mapRow(ResultSet resultSet, int i) throws SQLException { Book book=new Book(); book.setbName(resultSet.getString("bname")); book.setbId(resultSet.getInt("bid")); book.setbPrice(resultSet.getDouble("bprice")); return book; } }
7.编写dao实现类
package book.dao.impl; import book.dao.IBookDao; import book.entity.Book; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.support.JdbcDaoSupport; import org.springframework.stereotype.Repository; import javax.annotation.Resource; import java.util.List; /** * Created by Administrator on 2017/3/30/030. */ @Repository public class BookDaoImpl extends JdbcDaoSupport implements IBookDao {
添加图书 @Override public int addBook(Book book) { String sql="insert into book(bname,bprice) values (?,?)"; getJdbcTemplate().update(sql,book.getbName(),book.getbPrice()); return getJdbcTemplate().update(sql,book.getbName(),book.getbPrice()); } //查询全部图书 @Override public List<Book> bookList() { String sql="select * from book"; return getJdbcTemplate().query(sql,new BookRowMap()); } @Resource public void setJdbcTemplate1(JdbcTemplate jdbcTemplate){ super.setJdbcTemplate(jdbcTemplate); } }
8.编写service层接口
package book.service; import book.entity.Book; import java.util.List; /** * Created by Administrator on 2017/3/30/030. */ public interface IBookService { int addBook(Book book); List<Book> bookList(); }
9.编写service实现类
package book.service.impl; import book.dao.IBookDao; import book.entity.Book; import book.service.IBookService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** * Created by Administrator on 2017/3/30/030. */ @Service("service") public class BookServiceImpl implements IBookService { @Resource private IBookDao bookDao; @Override public int addBook(Book book) { return bookDao.addBook(book) ; } @Override public List<Book> bookList() { return bookDao.bookList(); } }
10.编写测试类
package book.test; import book.entity.Book; import book.service.IBookService; import book.service.impl.BookServiceImpl; import cn.lhl.Prienter.Prienter; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * Created by Administrator on 2017/3/30/030. */ public class BookTest { @Test public void add(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext3.xml"); IBookService service= (IBookService)context.getBean("service"); Book book =new Book(); book.setbName("深入Xml"); book.setbPrice(189.6); System.out.println(service.addBook(book)); } @Test public void info(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext3.xml"); IBookService service= (IBookService)context.getBean("service"); List<Book> book=service.bookList(); for (Book item: book) { System.out.println(item.getbName()); } } }