案例链接:链接: https://pan.baidu.com/s/1kPzTOknuRK6a8tPqT54REg
提取码: szr6
步骤1:
在src下新建一个applicationContext.xml文件,有几种配置数据库连接池的方式,以下有几种连接池可选择:Spring自带的连接池、DBCP 连接池以及C3P0 连接池,我们以C3P0连接池为例。
具体配置如下:
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注解扫描 -->
<context:component-scan base-package="com.hello"/>
<!-- Spring 自带的连接池
DriverClassName 用来配置驱动名字
Url 数据库
Username 用户名
Password 密码
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://10.25.161.7:3306/jss"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
-->
<!-- 使用 DBCP 连接池
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://10.25.161.7:3306/jss"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
-->
<!-- 使用 C3P0 连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/cz3"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置 JDBC 模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
步骤2:
在com.hello.pojo包下新建User类,字段和数据库表中的字段一样
package com.hello.pojo;
public class User {
private Integer id;
private String username;
private String password;
public User() {
super();
}
public User(Integer id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
步骤3:
在com.hello.mapper包下新建MyRowMapper类实现RowMapper接口,重写mapRow方法,指定返回User对象,这个在Dao层的时候查询返回对象或者对象集合需要用到。
package com.hello.Mapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import com.hello.pojo.User;
public class MyRowMapper implements RowMapper<User>{
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
//获取结果集中的数据
int id = resultSet.getInt("id");
String username = resultSet.getString("username");
String password = resultSet.getString("password");
//把数据封装成User对象
User user = new User();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
return user;
}
}
步骤4:
在com.hello.dao包下新建一个UserDao接口以及实现类UserDaoImpl
UserDao接口代码:
package com.hello.dao;
import java.util.List;
import com.hello.pojo.User;
public interface UserDao {
//通过id查找User
public User findUserById(Integer id);
//查询全部User
public List<User> selectAllUser();
//通过id查找username
public String findNameById(Integer id);
}
UserDaoImpl实现类代码:
(里面举例了查询返回结果的3种情况:返回某个值、返回某个对象、返回某个对象集合)
package com.hello.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import com.hello.Mapper.MyRowMapper;
import com.hello.pojo.User;
@Component("UserDao")
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
/* 查询返回某一个值!! */
// 通过id查找username
@Override
public String findNameById(Integer id) {
String sql = "select username from user where id = ?";
String name = jdbcTemplate.queryForObject(sql, String.class, id);
return name;
}
/* 查询返回单个对象!! */
// 通过id查找User
@Override
public User findUserById(Integer id) {
String sql = "select * from user where id = ?";
User user = jdbcTemplate.queryForObject(sql, new MyRowMapper(), id);
return user;
}
/* 查询返回对象集合!! */
// 查询全部User
@Override
public List<User> selectAllUser() {
String sql = "select * from user";
List<User> userList = jdbcTemplate.query(sql, new MyRowMapper());
return userList;
}
}
上面的出现的
@Autowired
private JdbcTemplate jdbcTemplate;
是自动装载xml文件中的bean类,得到一个Spring的JdbcTemplate对象,通过这个对象可以调用函数进行增删改查等操作。
步骤5:
完成以上步骤就可以进行测试了,新建测试类SpringTest,分别对Dao层的三个方法进行测试
package com.hello.test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.hello.dao.UserDao;
import com.hello.pojo.User;
@ContextConfiguration("classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringTest {
@Autowired
private UserDao userDao;
//通过id查找User
@Test
public void findUserById() {
User user = userDao.findUserById(1);
System.out.println(user);
}
//查询全部User
@Test
public void selectAllUser(){
List<User> userList = userDao.selectAllUser();
for (User user : userList) {
System.out.println(user);
}
}
//通过id查找username
@Test
public void findNameById(){
String name = userDao.findNameById(1);
System.out.println(name);
}
}
测试运行findUserById()方法,运行结果: