导入Maven依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring-version>5.0.2.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
</dependencies>
编写实体类
public class Person {
private String pId;
private String pName;
private String page;
public Person() {
}
public Person(String pId, String pName, String page) {
this.pId = pId;
this.pName = pName;
this.page = page;
}
}
编写dao
import com.wlp.pojo.Person;
public interface PersonDao {
int insertPerson(Person p) throws Exception;
}
package com.wlp.dao.impl;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.wlp.dao.PersonDao;
import com.wlp.pojo.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class PersonDaoImpl implements PersonDao {
private ComboPooledDataSource dataSource;
public void setDataSource(ComboPooledDataSource dataSource) {
this.dataSource = dataSource;
}
public int insertPerson(Person p) throws Exception{
Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement("insert into person values(?,?,?)");
ps.setString(1,p.getpId());
ps.setString(2,p.getpName());
ps.setString(3,p.getPage());
return ps.executeUpdate();
}
}
编写Service
public interface PersonService {
int save(Person p);
}
public class PersonServiceImpl implements PersonService {
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public int save(Person p) {
try {
return personDao.insertPerson(p);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
编写properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_wlp?serverTimezone=UTC
jdbc.user=root
jdbc.password=xxxx
编写Spring配置文件
<?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"
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">
<bean id="person" class="com.wlp.pojo.Person"/>
<bean id="personDaoImpl" class="com.wlp.dao.impl.PersonDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="personServiceImpl" class="com.wlp.service.impl.PersonServiceImpl">
<property name="personDao" ref="personDaoImpl"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
测试
@Test
public void personTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonServiceImpl ps = context.getBean("personServiceImpl", PersonServiceImpl.class);
System.out.println(ps.save(new Person()));
}