终于可以连接数据库来操作数据了!!!!!!
首先搭建Beans层
public class Book { private int bookId; private String bookName; private int bookPrice; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public int getBookPrice() { return bookPrice; } public void setBookPrice(int bookPrice) { this.bookPrice = bookPrice; } }
然后搭建dao
public interface IBookDAO { public int addBook(Book book); } @Service("bookService") //这个层的 public class BookServiceImpl implements IBookService { //得去引入/引用/调度/植入 //jdbc的 @Autowired private IBookDAO dao; public int addBook(Book book) { int count= dao.addBook(book); return count; } public IBookDAO getDao() { return dao; } public void setDao(IBookDAO dao) { this.dao = dao; } }
service
public interface IBookService { public int addBook(Book book); } @Service("bookService") //这个层的 public class BookServiceImpl implements IBookService { //得去引入/引用/调度/植入 //jdbc的 @Autowired private IBookDAO dao; public int addBook(Book book) { int count= dao.addBook(book); return count; } public IBookDAO getDao() { return dao; } public void setDao(IBookDAO dao) { this.dao = dao; } }
<context:property-placeholder location="jdbc.properties"/> <!--00.开启包扫描器--> <context:component-scan base-package="cn.jdbc"></context:component-scan> <!--1.配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--2.jdbcTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///book jdbc.username=root jdbc.password=1234