发表于: 2022-05-14 19:20:35
1 640
今天尝试不抄网上的代码,借鉴一下自己尝试写。
创建jdbctemplate:
package com.weixiao;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcTemplateTest implements BiageDao {
@Autowired
//注入jdbc
private JdbcTemplate jdbcTemplate;
@Override
@Test
public int sayHello(Biage bg) {
String sql="INSERT INTO book(name,type,id) VALUES(?,?,?)";
Object[] args={bg.getName(),bg.getType(),bg.getId()};
int update = jdbcTemplate.update(sql, args);
return update;
}
}
package com.weixiao;
public class BiageServic {
private Biage biage;
public void setBiage(Biage biage) {
this.biage = biage;
}
public int addBook(Biage biage){
System.out.println("添加业务逻辑处理。。。");
return Biage.sayshllo(biage);
}
}
package com.weixiao;
import java.io.Serializable;
class Biage implements Serializable {
private Integer Id;
private String Name;
private String Type;
private String School;
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
public String getSchool() {
return School;
}
public void setSchool(String school) {
School = school;
}
private String School;
public Biage(){
}
@Override
public String toString() {
return "biage{" +
"id=" + Id +
", name='" + Name + '\'' +
", type=" + Type +
", school='" + School + '\'' +
'}';
}
}
配置pom文件:
<?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>weixiao01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>weixiao01 Maven Webapp</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>
<!--spring框架 整合junit -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--spring框架 aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- DButils驱动-->
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
</dependency>
<!-- c3p0驱动-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>weixiao01</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
创建一个jdbctemplate.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/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--把testDao对象的创建权交给Spring-->
<!--表示配置扫描路径选项-->
<context:annotation-config />
<context:component-scan base-package="com.weixiao"/>
<!--配置bean,配置后该类由spring管理-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--jdbc连接数据库配置连接池-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="adbrk"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/fc"></property>
<property name="checkoutTimeout" value="1000"></property>
<property name="initialPoolSize" value="100"></property>
<!--缓存池最大值-->
<property name="maxPoolSize" value="1000"></property>
<!--缓存池最小值-->
<property name="minPoolSize" value="50"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--系统默认配置的连接池-->
<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="root"></property>
<property name="password" value="adbrk"></property>
<property name="url" value="jdbc:mysql://localhost:3306/fc"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
遇到问题:
代码编写时候,单段代码真的意思,可要连起来就不太理解了。
评论