代码功能:向数据库mydata的classmate表中写入一条记录<13 孙燕姿 新加坡>
代码构成:
代码思想:
1、构建实体类Classmate,属性加入set、get方法。
package wednesday.domain;
public class Classmate {
private int deptno;
private String dename;
private String loc;
public Classmate(){}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDename() {
return dename;
}
public void setDename(String dename) {
this.dename = dename;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
2、service调用dao(在spring中的解释就是service依赖注入dao)
dao操作数据库,此时利用依赖注入的思想注入datasource
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydata" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="initialSize" value="5" />
</bean>
<bean id="classmatedao"
class="wednesday.dao.Classmatedao">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="classmateservice"
class="wednesday.service.Classmateservice">
<property name="superdao" ref="classmatedao"/>
</bean>
</beans>
package wednesday.service;
import wednesday.dao.Super.*;
import wednesday.domain.Classmate;
public class Classmateservice {
private Superdao superdao;
public Superdao getSuperdao() {
return superdao;
}
public void setSuperdao(Superdao superdao) {
this.superdao = superdao;
}
public void add(Classmate cm){
superdao.save(cm);
}
}
package wednesday.dao;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import wednesday.dao.Super.Superdao;
import wednesday.domain.Classmate;
public class Classmatedao implements Superdao{
private DataSource dataSource;
public void save(Classmate cm) {
try {
Connection conn=dataSource.getConnection();
conn.createStatement().executeUpdate("insert into "
+ "classmate values("+cm.getDeptno()+",'"+cm.getDename()+"','"+cm.getLoc()+"')");
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}
3、写测试类test
Spring容器中并没有classmate的bean配置。这是我们需要加进来的对象new一个出来
利用ApplicationContext取得service的bean,然后由Spring自动调用dao来操作数据库。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import wednesday.domain.Classmate;
import wednesday.service.Classmateservice;
public class test {
public static void main(String []args)throws Exception{
Classmate u=new Classmate();
u.setDeptno(13);
u.setDename("孙燕姿");
u.setLoc("新加坡");
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
Classmateservice service=(Classmateservice)ctx.getBean("classmateservice");
service.add(u);
}
}
运行结果:
代码下载链接:http://yun.baidu.com/s/1sjytr3J