package com.dao;
public class Customer {
int customer;
String name;
int age;
public Customer() {
}
@Override
public String toString() {
return "Customer{" +
"customer=" + customer +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Customer(int customer, String name, int age) {
this.customer = customer;
this.name = name;
this.age = age;
}
public int getCustomer() {
return customer;
}
public void setCustomer(int customer) {
this.customer = customer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.dao;
public interface CustomerDAO {
public void insert(Customer customer);
public Customer findByCustomerId(int custId);
}
package com.dao;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcCustomerDAO implements CustomerDAO{
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void insert(Customer customer){
String sql="INSERT INTO CUSTOMER " +"(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
Connection conn=null;
try{
conn=dataSource.getConnection();
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,customer.getCustomer());
ps.setString(2,customer.getName());
ps.setInt(3,customer.getAge());
ps.executeUpdate();
// System.out.println("写入!");
ps.close();
}catch (SQLException e){
throw new RuntimeException(e);
}finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}
}
@Override
public Customer findByCustomerId(int custId) {
String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?";
Connection conn=null;
try {
Customer cstomer=null;
conn=dataSource.getConnection();
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,custId);
ResultSet rs = ps.executeQuery();
if (rs.next()){
System.out.println("==========");
System.out.println(rs.getInt("CUST_ID"));
cstomer=new Customer(
rs.getInt("CUST_ID"),
rs.getString("NAME"),
rs.getInt("AGE")
);
}
rs.close();
ps.close();
return cstomer;
}catch (SQLException e){
throw new RuntimeException(e);
}finally {
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
package com.test1;
import com.dao.Customer;
import com.dao.CustomerDAO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testMain {
public static void main(String[] args){
// ApplicationContext acx=new ClassPathXmlApplicationContext("Bean.xml");
// IntrduceDemo id=acx.getBean("1",IntrduceDemo.class);
// id.setAge(54);
// id.intrduce();
// IntrduceDemo id1=acx.getBean("1",IntrduceDemo.class);
// id1.intrduce();
ApplicationContext context=new ClassPathXmlApplicationContext("Spring-Module.xml");
CustomerDAO customerDAO= (CustomerDAO) context.getBean("customerDao");
Customer customer=new Customer(13,"香宝",22);
customerDAO.insert(customer);
Customer customer1=customerDAO.findByCustomerId(2);
System.out.println(customer1.toString());
}
}
<?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.xsd">
<bean id="customerDao" class="com.dao.JdbcCustomerDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
<?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.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring_test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
</beans>
<?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.xsd">
<import resource="/Spring-Datasource.xml" />
<import resource="/Spring-Customer.xml" />
</beans>