标签:connect ica 逻辑 exce final xmlns oca 下载 upd
我们是使用的是maven,我们下载节点即可。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.3.RELEASE</version> </dependency> <!--spring-jdbcjar 包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.3.RELEASE</version> </dependency> <!--commons-dncpjar 包--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.1.1</version> </dependency> <!--c3p0jar 包--> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!--mysql数据库驱动--> <dependency> <groupId>org.wisdom-framework</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34_1</version> </dependency>
(1)创建分层 beans层(实体类) dao(表示层) biz(业务逻辑层) web(数据展示) util(工具类层)
BEANS:
public class Book {
private Integer id; private String name; private String price; public Book() { } public Book(String name, String price) { this.name = name; this.price = 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 String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }
DAO:
public interface BookDao { int add(Book book); List<Book> selectAll(); }
使用注解的方式实现方法的实现层impl
@Repository public class BookDaoImpl extends JdbcDaoSupport implements BookDao { public int add(Book book) { int count = this.getJdbcTemplate().update("insert into book(name,price) values(?,?)",book.getName(),book.getPrice()); return count; } public List<Book> selectAll() { return this.getJdbcTemplate().query("select * from book",new MyRowMapper()); } @Autowired public void setJdbcTemplate2(JdbcTemplate jbdcTemplate){ super.setJdbcTemplate(jbdcTemplate); } }
你可能会问为啥要定义setJdbcTemplate2()方法呢?
public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; initTemplateConfig(); } @Override protected void checkDaoConfig() { if (this.jdbcTemplate == null) { throw new IllegalArgumentException("‘dataSource‘ or ‘jdbcTemplate‘ is required"); } }
该方法源自父类的方法,并且是经过final修饰,我们都知道被final修饰的方法是不能被修改的,当我们自己不重新定义方法,而是使用相同的方法名时,会出现错误。
BIZ:
public interface BookBiz { int add(Book book); List<Book> selestAll(); }
Biz层方法的实现层impl:
@Service("bookBiz") public class BookBizImpl implements BookBiz { @Resource private BookDao dao; public int add(Book book) { return dao.add(book); } public List<Book> selestAll() { return dao.selectAll(); } }
public class FirstTest { @Test public void findTwo(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContexttemp.xml"); BookBiz proxy=(BookBiz) ctx.getBean("bookBiz"); int count = proxy.add(new Book("hello ", "45")); System.out.println(count); } @Test public void findOne(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContexttemp.xml"); BookBiz proxy=(BookBiz) ctx.getBean("bookBiz"); List<Book> list = proxy.selestAll(); for (Book item : list) { System.out.println(item.getName()); } } }
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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置数据源-->
<!--spring 内置--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--c3p0数据源--> <!--<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>--> <!--定义dbcp数据源--> <!--<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>-->
先这么多吧。。。。。。。。。。。。
标签:connect ica 逻辑 exce final xmlns oca 下载 upd
原文地址:http://www.cnblogs.com/cuntouyixiaohuo/p/6663808.html