> 文章列表 > Javaee Spring JdbcTemplate基本使用查询数据库表的内容 基于xml配置方式

Javaee Spring JdbcTemplate基本使用查询数据库表的内容 基于xml配置方式

Javaee Spring JdbcTemplate基本使用查询数据库表的内容 基于xml配置方式

目录

哈哈哈哈,说好是要写一篇关于jdbcTemplate的基本使用,貌似说跑题了,但是主体还是用jdbctemplate实现的,有耐心看完的话相信能有点点收获的哦!

 项目结构:

 用到的数据库:

 小结:

遇到了个小问题,数据库为8.0版本需要更换spring.xml的配置方式

1.driver,需要改成

2.url需要拼接

解决方式: 用资源文件注入

举例:假设如下登录界面,点击登录,假设登录失败,会跳转一个失败界面,这是怎么回事呢?

 底层逻辑:页面发出请求,将数据给到表现层,再由表现层给到业务层,再由业务层给到数据访问层,再由数据访问层访问数据库是否存在该数据,两种结果,存在或者不存在,此时会在把这个数据按顺序传回页面

由于我们没学表现层,将数据拿至测试类中,该怎么实现呢?

​编辑 运行成功!


哈哈哈哈,说好是要写一篇关于jdbcTemplate的基本使用,貌似说跑题了,但是主体还是用jdbctemplate实现的,有耐心看完的话相信能有点点收获的哦!

 项目结构:

导入jar包:

 用到的数据库:

 

AccountDao.java

package wwx.dao;import wwx.domain.Account;import java.util.List;public interface AccountDao {//查询所有public List<Account> findAll();
}

 AccountDaoImpl.java

package wwx.dao;import wwx.domain.Account;import java.util.List;public class AccountDaoImpl implements AccountDao {//查询所有@Overridepublic List<Account> findAll() {System.out.println("我是Dao...");return null;}
}

Account.java

package wwx.domain;public class Account{private String username;private int password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public int getPassword() {return password;}public void setPassword(int password) {this.password = password;}@Overridepublic String toString() {return "Account{" +"username='" + username + '\\'' +", password=" + password +'}';}
}

 AccountServie.java

package wwx.service;import wwx.domain.Account;import java.util.List;public interface AccountService {//查询所有public List<Account> findAll();
}

 AccountServieImpl.java

package wwx.service;
import wwx.dao.AccountDao;
import wwx.dao.AccountDaoImpl;
import wwx.domain.Account;import java.util.List;public class AccountServiceImpl implements AccountService {@Overridepublic List<Account> findAll() {System.out.println("我是service...");AccountDao accountDao=new AccountDaoImpl();accountDao.findAll();return null;}
}

JDBCTest.java

package wwx.test;
import org.junit.Test;
import wwx.dao.AccountDao;
import wwx.domain.Account;
import wwx.service.AccountService;
import wwx.service.AccountServiceImpl;import java.util.List;public class JDBCTest {@Testpublic void test01(){   //在test中创建了一个业务层对象,用业务层对象调用业务层中的调用方法,// 此时业务层方法中创建了Dao对象,调用了Dao方法//这样写的话,类与类直接耦合度太高了,// 举个例子,假设Dao包下的AccountDaoImpl突然没有了,AccountServiceImpl中代码就会报错//该如何解决呢,使用Spring:解耦,降低类内之间的联系,//也就是不用在AccountServiceImpl中去new Dao ,让Spring去new,如果要用到,通过注入方式注入进来AccountService accountService=new AccountServiceImpl();accountService.findAll();}}

 小结:

 //在test中创建了一个业务层对象,用业务层对象调用业务层中的调用方法,// 此时业务层方法中创建了Dao对象,调用了Dao方法//但是这样写的话,类与类直接耦合度太高了,// 举个例子,假设Dao包下的AccountDaoImpl突然没有了,AccountServiceImpl中代码就会报错//该如何解决呢,使用Spring:解耦,降低类内之间的联系,//也就是不用在AccountServiceImpl中去new AccountDao ,让Spring去new,如果要用到,通过注入方式注入进来

jdbcTest.java

package wwx.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import wwx.dao.AccountDao;
import wwx.domain.Account;
import wwx.service.AccountService;
import wwx.service.AccountServiceImpl;import java.util.List;public class JDBCTest {@Testpublic void test01(){   //在test中创建了一个业务层对象,用业务层对象调用业务层中的调用方法,// 此时业务层方法中创建了Dao对象,调用了Dao方法//这样写的话,类与类直接耦合度太高了,// 举个例子,假设Dao包下的AccountDaoImpl突然没有了,AccountServiceImpl中代码就会报错//该如何解决呢,使用Spring:解耦,降低类内之间的联系,//也就是不用在AccountServiceImpl中去new Dao ,让Spring去new,如果要用到,通过注入方式注入进来
//        AccountService accountService=new AccountServiceImpl();
//        accountService.findAll();//加载配置文件ApplicationContext app=new ClassPathXmlApplicationContext("Spring.xml");//获得bean//spring容器中id唯一//需要强转AccountService accountService = (AccountService) app.getBean("accountService");//调用方法accountService.findAll();//此时删除AccountServiceImpl,编译不会报错,但是无法运行,耦合不能消除,但能降低}}

Spring.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/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--service层--><bean id="accountService" class="wwx.service.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property></bean><!--Dao层--><bean id="accountDao" class="wwx.dao.AccountDaoImpl"></bean>
</beans>

//此时删除AccountServiceImpl,编译不会报错,但是无法运行,耦合不能消除,但能降低

 借助Dao层访问数据库,两种技术,jdbctemplate,mybabties,此时用jdbctemplate

但是这样又回到之前的,在类中new,改用注入由于数据库为8.0版本运行失败

遇到了个小问题,数据库为8.0版本需要更换spring.xml的配置方式

1.driver,需要改成

<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>

2.url需要拼接

useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&characterEncoding=utf-8
<property name="jdbcUrl" value="mysql://localhost:3306/wwx?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&characterEncoding=utf-8"></property>

但是xml文件中无法识别&所以改用资源文件注入

解决方式: 用资源文件注入

Spring.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/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!--加载属性配置文件--><context:property-placeholder location="classpath:jdbc.properties"/>
<!--上面这行是在末尾加了个/成了单标签,原本是一对的--><!--数据源--><!--遇到了个小问题,及数据库为8.0版本需要更换spring.xml的配置方式--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!--service层--><bean id="accountService" class="wwx.service.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property></bean><!--Dao层--><bean id="accountDao" class="wwx.dao.AccountDaoImpl"><property name="jdbcTemplate" ref="jdbcTemplate"></property><!--name中jdbcTemplate,是AccountDaoImpl类中的成员属性,ref中jdbcTemplate是jdbcTemplate层的id--></bean><!--JdbcTempalte层--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property><!--注入--></bean></beans>

jdbc.propertities

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/wwx?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

此时到JDBCTest.java中运行test01发现成功访问数据库

举例:假设如下登录界面,点击登录,假设登录失败,会跳转一个失败界面,这是怎么回事呢?

 底层逻辑:页面发出请求,将数据给到表现层,再由表现层给到业务层,再由业务层给到数据访问层,再由数据访问层访问数据库是否存在该数据,两种结果,存在或者不存在,此时会在把这个数据按顺序传回页面

由于我们没学表现层,将数据拿至测试类中,该怎么实现呢?

原理:传递参数时是通过方法的参数值传递,返回数据时,是通过方法的返回值往回返

 

 

 运行成功!