JdbcTemplate
Jar包
mysql-connector-java
(下载Mysql时自带 去安装文件里找 例如:C:\Program Files (x86)\MySQL\Connector J 8.0)
commons-logging
(https://commons.apache.org/proper/commons-logging/download_logging.cgi)
spring-aop
spring-beans
spring-context
spring-core
spring-jdbc
spring-tx
(https://repo.spring.io/webapp/#/artifacts/browse/simple/General/libs-release-local/org/springframework/spring/5.0.2.RELEASE/spring-framework-5.0.2.RELEASE-dist.zip)
配置文件
<?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">
<context:component-scan base-package="你的包名"/>
<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/要连接的database名字?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT"/>
<property name="username" value="你的用户名"/>
<property name="password" value="你的密码"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="testDao" class="ch5.TestDaoImpl"/>
</beans>
注意XML里不能使用& 用&;
代替
配置文件将DataSource注入到了JdbcTemplate中,之后直接使用jdbcTemplate中的update和query函数对表进行增删改查即可
实现
实体类
要求属性和表字段相同
public class MyUser {
private int uid;
private String uname;
private String usex;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUsex() {
return usex;
}
public void setUsex(String usex) {
this.usex = usex;
}
@Override
public String toString() {
return "MyUser [uid=" + uid + ", uname=" + uname + ", usex=" + usex + "]";
}
}
访问层
import java.util.List;
public interface TestDao {
public int update(String sql,Object[] param);
public List<MyUser> query(String sql,Object[] param);
}
---------------------------------
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
public class TestDaoImpl implements TestDao {
@Autowired
private JdbcTemplate jdbc;
@Override
public int update(String sql, Object[] param) {
return jdbc.update(sql,param);
}
@Override
public List<MyUser> query(String sql, Object[] param) {
// TODO Auto-generated method stub
RowMapper<MyUser> rowMapper=new BeanPropertyRowMapper<MyUser>(MyUser.class);
return jdbc.query(sql,rowMapper,param);
}
}
测试类
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpringJDBC {
public static void main(String[] args) {
ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
TestDao td=(TestDao)appCon.getBean("testDao");
String sql="insert into user values(null,?,?)";
Object[] p1= {"p1","男"};
Object[] p2= {"p2","男"};
Object[] p3= {"p3","男"};
Object[] p4= {"p4","男"};
Object[] p5= {"p5","男"};
td.update(sql, p1);
td.update(sql, p2);
td.update(sql, p3);
td.update(sql, p4);
td.update(sql, p5);
sql="select *from user";
List<MyUser> list=td.query(sql, null);
for(MyUser mu:list)
System.out.println(mu);
}
}
结果
MyUser [uid=1, uname=p1, usex=男]
MyUser [uid=2, uname=p2, usex=男]
MyUser [uid=3, uname=p3, usex=男]
MyUser [uid=4, uname=p4, usex=男]
MyUser [uid=5, uname=p5, usex=男]