了解知道即可
JdbcTemplate环境配置
先加入依赖:
在pom.xml中要引入spring和mysql的依赖:
<!--仓库和依赖--><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.0.0-M2</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>6.0.0-M2</version></dependency><!-- mysql jar包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.30</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies>
创建一个完整的javabean类:
有数据库的表的格式一致。
package com.hei.bean;public class User {private Integer id;private String real_name;private Integer age;public User(){}public User(Integer id, String real_name, Integer age) {this.id = id;this.real_name = real_name;this.age = age;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getReal_name() {return real_name;}public void setReal_name(String real_name) {this.real_name = real_name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User{" +"id=" + id +", real_name='" + real_name + '\'' +", age=" + age +'}';}
}
创建一个数据源,用于mysql的连接:
接口实现DataSource,要重写所有的方法,在连接方法中,要创建驱动,获取数据库连接对象。
package com.hei.bean;import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.*;
import java.util.concurrent.Callable;
import java.util.logging.Logger;public class MyDateSorce implements DataSource {private String diver;private String url;private String username;private String password;public void setDiver(String diver) {this.diver = diver;}public void setUrl(String url) {this.url = url;}public void setUsername(String username) {this.username = username;}public void setPassword(String password) {this.password = password;}@Overridepublic Connection getConnection() throws SQLException {//注册驱动try {Class.forName(diver);//获取数据库连接对象Connection c=DriverManager.getConnection(url,username,password);return c;} catch (Exception e) {e.printStackTrace();}return null;}@Overridepublic Connection getConnection(String username, String password) throws SQLException {return null;}@Overridepublic PrintWriter getLogWriter() throws SQLException {return null;}@Overridepublic void setLogWriter(PrintWriter out) throws SQLException {}@Overridepublic void setLoginTimeout(int seconds) throws SQLException {}@Overridepublic int getLoginTimeout() throws SQLException {return 0;}@Overridepublic ConnectionBuilder createConnectionBuilder() throws SQLException {return DataSource.super.createConnectionBuilder();}@Overridepublic Logger getParentLogger() throws SQLFeatureNotSupportedException {return null;}@Overridepublic ShardingKeyBuilder createShardingKeyBuilder() throws SQLException {return DataSource.super.createShardingKeyBuilder();}@Overridepublic <T> T unwrap(Class<T> iface) throws SQLException {return null;}@Overridepublic boolean isWrapperFor(Class<?> iface) throws SQLException {return false;}
}
在配置文件(spring.xml)中,进行数据源的配置:
<!-- 配自己写的数据源--><bean id="db" class="com.hei.bean.MyDateSorce"><property name="diver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/spring6"/><property name="username" value="root"/><property name="password" value="123456"></property></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="db"></property></bean>
测试类中,对数据库的增加一个对象:
在这只演示一个增加方法:
public class Test {@org.junit.Testpublic void test(){ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");JdbcTemplate j= applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);//System.out.println(j);//insert语句String sql=" insert into t_table(id,real_name,age) values(?,?,?)";j.update(sql,4,"李明",23);}
}