目录
代码展示
实体User
test:测试
JdbcTemplateCRUDTest
JdbcTemplateTest
resources:配置文件
applicationContext.xml
jdbc.properties
pom.xml
代码展示
实体User
package org.ljx.domain; public class User { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
test:测试
JdbcTemplateCRUDTest
package org.ljx.test; import org.junit.Test; import org.junit.runner.RunWith; import org.ljx.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class JdbcTemplateCRUDTest { @Autowired private JdbcTemplate jdbcTemplate; @Test public void testUpdate(){ jdbcTemplate.update("update user set password=? where name=?",1,"li"); } @Test public void testDelete(){ jdbcTemplate.update("delete from user where name=?","li"); } @Test public void testQueryAll(){ List<User> userList = jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<User>(User.class)); System.out.println(userList); } @Test public void testQueryOne(){ User userList = jdbcTemplate.queryForObject("select * from user where name =?", new BeanPropertyRowMapper<User>(User.class),"王"); System.out.println(userList); } @Test public void testQueryCount(){ Long count = jdbcTemplate.queryForObject("select count(*) from user ", long.class); System.out.println(count); } }
JdbcTemplateTest
package org.ljx.test; import com.alibaba.druid.pool.DruidDataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; import java.beans.PropertyVetoException; public class JdbcTemplateTest { @Test // 测试JdbcTemplate开发步骤 public void test01() throws PropertyVetoException { // 创建数据源对象 ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test"); dataSource.setUser("root"); dataSource.setPassword("asd.123"); JdbcTemplate jdbcTemplate = new JdbcTemplate(); // 设置数据源对象 知道数据源在哪 jdbcTemplate.setDataSource(dataSource); // 执行操作 jdbcTemplate.update("insert into user values(?,?)","li",12); } @Test // 测试Spring产生jdbcTemplate对象 public void test02() throws PropertyVetoException { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class); int i = jdbcTemplate.update("insert into user values (?,?)", "jagssdtfy", 123456); System.out.println(i); } }
resources:配置文件
applicationContext.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" 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"> <!-- 加载jdbc.properties--> <context:property-placeholder location="classpath: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.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- jdbc模板--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=asd.123
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring_jdbc</artifactId> <version>1.0-SNAPSHOT</version> <name>spring_jdbc</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <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> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.12.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.12.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.23</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.12.RELEASE</version> </dependency> </dependencies> </project>