jdbcTemplate连接数据库就是用jdbcTemplate对象去调用它的query、udate、insert等方法操作数据库。
jdbcTemplate必须有dao层实现类,因为jdbcTemplate是在dao层用jdbcTemplate对应的方法操作sql语句的
本案例使用IDEA创建的maven的web项目
<!--声明springboot版本-->
<parent><!--注意该标签不在<dependencies>中-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- mvc,aop的依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 由于我们在上面指定了parent,这里就不需要指定版本号 -->
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<!-- 阿里巴巴的Druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!--pageHelper分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
</dependency>
<!--quartz依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
#primary表名第一个数据库
spring.datasource.primary.url=jdbc:mysql://localhost:3306/day06
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
#secondary表名第二个数据库
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/stage3
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
该案例将该类命名为:DatasourceConfig
package com.qf.util;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
//Configuration注解的类一一是代替ssm中的xml配置文件,以类代替文件
@Configuration//表名此类是是配置工具类,让springboot框架将此类当成配置文件加载
public class DatasourceConfig {
//创建第一个数据源(数据库)
@Bean//bean注解用来创建数据源,Bean注解几乎是方法专用。//默认对象名就是方法名,但可以用Bean(name="onetemplate")进行改变。但对应的位置也要改变)
@Primary//表示该数据源为首选数据源.主要用于分清主次数据源
@ConfigurationProperties(prefix ="spring.datasource.primary" )//ConfigurationProperties用来连接创建谁的数据源对象。spring.datasource.primary是第一个数据库的公共部分,表名第一个数据库
public DataSource first(){
return DataSourceBuilder.create().build();
}
//创建第二个数据源(数据库)
@Bean //方法名就是对象名
@ConfigurationProperties(prefix ="spring.datasource.secondary")
public DataSource two(){
return DataSourceBuilder.create().build();
}
}
package com.qf.util;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
//数据源具体怎么使用就依赖于这个类
//该类作用:给jdbcTemplate注入不同的数据源
@Configuration
public class JdbcTemplateConfig {
@Bean
//如果DatasourceConfig用Bean(name="onetemplate")进行改变,这里的first也要改变。我是这么想的。
public JdbcTemplate onetemplate(@Qualifier("first") DataSource dataSource){//@Qualifier("first")指定第一个数据源。first为数据源对应的方法名。不指定数据源,由于有两个,因此会报错
return new JdbcTemplate(dataSource);
}
@Bean
public JdbcTemplate twotemplate(@Qualifier("two") DataSource dataSource){//@Qualifier("first")指定第二个数据源。first为数据源对应的方法名
return new JdbcTemplate(dataSource);
}
}
package com.qf.entity;
import java.util.Date;
public class Users {
private Integer userId;
private String username;
private Date birthday;
private Integer age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Users{" +
"userId=" + userId +
", username='" + username + '\'' +
", birthday=" + birthday +
", age=" + age +
'}';
}
}
该案例将该接口命名为:UsersDao
package com.qf.dao;
import com.qf.entity.Users;
import java.util.List;
//mybatis框架操作数据库有两种方式,一种是配置文件方式,一种是注解方式
public interface UsersDao {
public List<Users> getall();
}
该案例将该实现类命名为:UsersDaoImpl
package com.qf.dao.impl;
import com.qf.dao.UsersDao;
import com.qf.entity.Users;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository//Repository给当前类创建对象
public class UsersDaoImpl implements UsersDao {
@Resource(name = "onetemplate")
private JdbcTemplate jdbcTemplate;
@Override
public List<Users> getall() {
List<Users> userList = jdbcTemplate.query("SELECT * FROM users", new RowMapper<Users>() {
@Override
public Users mapRow(ResultSet resultSet, int i) throws SQLException {
Users users = new Users();
users.setAge(resultSet.getInt("age"));//第二个age是实体类Users的age属性对应的表的字段名。反正第二个age是字段名,第一个age是实体类属性
users.setBirthday(resultSet.getDate("birthday"));
users.setUsername(resultSet.getString("username"));
users.setUserId(resultSet.getInt("userId"));
return users;
}
});
return userList;
}
}
该案例将该接口命名为:UsersService
package com.qf.service;
import com.qf.entity.Users;
import java.util.List;
public interface UsersService {
public List<Users> getall();
}
该案例将该实现类命名为:UsersServiceImpl
package com.qf.service.impl;
import com.qf.dao.UsersDao;
import com.qf.entity.Users;
import com.qf.service.UsersService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UsersServiceImpl implements UsersService{
@Resource
private UsersDao usersDao;
@Override
public List<Users> getall() {
return usersDao.getall();
}
}
该案例将该类命名为:TestController
package com.qf.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Users;
import com.qf.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class TestController {
@Resource
private UsersService usersService;
@RequestMapping("/test")//访问此请求的地址是localhost:8080/test
public String test( ModelMap map){
System.out.println("testjsp-----------");
List<Users> usersList = usersService.getall();
System.out.println("userList:"+usersList);
map.addAttribute("usersList",usersList);
return "show";//此时实际跳去的页面时/show.jsp.
}
}
package com.qf.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Users;
import com.qf.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class TestController {
@Resource
private UsersService usersService;
@RequestMapping("/test")//访问此请求的地址是localhost:8080/test
public String test( ModelMap map){
System.out.println("testjsp-----------");
List<Users> usersList = usersService.getall();
System.out.println("userList:"+usersList);
map.addAttribute("usersList",usersList);
return "show";//此时实际跳去的页面时/show.jsp.
}
}
该案例将该页面命名为:show.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html; charset=utf-8" pageEncoding="UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>TestController的test请求跳转成功</h1>
<h1></h1>
<c:forEach items="${usersList}" var="Users">
${Users.username}---${Users.birthday}---${Users.userId}
</c:forEach>
</body>
</html>