时间:2021-07-01 10:21:17 帮助过:10人阅读
由Spring框架给我们提供,Spring提供的很多操作数据源(关系型数据库,二维表格模型,有明确的行和列(mysql/orcal等) 非关系型数据库(redis.mongodb)NoSQL)消息列(activeMq,jms)的小工具
JdbcTemplate操作关系数据库
RedisTemplate操作redis
JmsTtemplate操作消息队列
JdbcTemplate类
使用方法和QueryRunner基本一致.
构造方法传递数据源DataSource对象
API方法:
update(String sql,Object...obj) 用来执行增删改操作
queryForObject(String sql,RowMapper mapper,Object...obj ) 查询返回单个对象.
queryForObject(String sql, Class cla,Object...obj)查询返回单个对象基本类型及其包装类和字符串
query(String sql,RowMapper mapper,Object...obj)查询返回集合对象
其中RowMapper是一个接口实现类
BeanPropertyRowMapper,查询的结果集封装,使用单个对象或者集合.
JdbcTemplate类实现account表的curd
使用的是Maven中的quickstart骨架创建的项目
引入的依赖为:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.itheima</groupId> <artifactId>spring_04_qm04</artifactId> <version>1.0-SNAPSHOT</version> <name>spring_04_qm04</name> <!-- FIXME change it to the project‘s website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies>
<!--使用Junit4测试jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--使用dbcp2连接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.4.0</version>
</dependency>
<!--引入jdbcTemplate-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!--引入mysql数据库连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<!--引入spring框架-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--junit使用12版本,不然会报错.-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
文件结构为:
pojo层为:
public class Account {
private int id;
private String name;
private double money;
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name=‘" + name + ‘\‘‘ +
", money=" + money +
‘}‘;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
从这里就可以看出来,这只是一个简单的举例,并没有使用业务层,直接从dao‘调用的方法,数据库设计的也是十分简陋
dao层的代码为
import java.util.List; public interface AccountDao { // 增加 void saveAccount(Account acount); }
dao层的实现类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import java.util.List; //使用注解的方法进行创建对象 @Repository("accountDao") public class AccountDaoImpl implements AccountDao { // 需要使用JdbcTemplate进行操作数据库 // 使用注解的方法,进行注入 @Autowired @Qualifier("jdbcTemplate") private JdbcTemplate jdbcTemplate; @Override public void saveAccount(Account account) { String sql = "insert into account values(?,?,?)"; jdbcTemplate.update(sql,account.getId(),account.getName(),account.getMoney()); } }
在这里需要解释一下各个标签的用处
1.注解@Controller为我们的控制器action类的类注解相当于applicationContext.xml文件中的bean节点,而括号中的值相当于bean节点中的id属性的属性值。
同理:@Service为我们业务层的类注解,@Repository为数据层dao的类注解。
2.@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法,简化程序代码。
当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。
3.@Qualifier("jdbcTemplate")确保唯一性
applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns 代表 xml namespace, 就是 XML 标签的命名空间。 -->
<!--xmlns:context 代表使用 context 作为前缀的命名空间-->
<!--xmlns:xsi 代表是指 XML 文件遵守的 XML 规范。 -->
<!--xsi:schemaLocation 代表 XML 标签遵守的 XML 规范。-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="包名"></context:component-scan>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--通过set方法的方式进行注入-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置操作数据库的Jdbcemplaate操作数据库-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--通过构造参数的方式进行注入-->
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
</beans>
<context:component-scan base-package="com.itheima"></context:component-scan> 关于这个标签:
通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,
配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,
如果扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,
则把这些类注册为bean
注:在注解后加上例如@Component(value=”abc”)时,注册的这个类的bean的id就是adc.
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) //整合Junit4测试时,用来引入多个配置文件时候,使用 @ContextConfiguration(locations = "classpath:applicationContext.xml") public class MainTest { @Autowired @Qualifier("accountDao") private AccountDao accountDao; @Test public void saveTest(){ Account account = new Account(); account.setName("孙"); account.setId(3); account.setMoney(1000); accountDao.saveAccount(account); } }
@RunWith就是一个运行器
@RunWith(JUnit4.class)就是指用JUnit4来运行
@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境
@RunWith(Suite.class)的话就是一套测试集合
@ContextConfiguration Spring整合JUnit4测试时,使用注解引入多个配置文件
单个文件
@ContextConfiguration(Locations="classpath:applicationContext.xml")
@ContextConfiguration(classes = SimpleConfiguration.class)
多个文件时,可用
@ContextConfiguration(locations = { "classpath:spring1.xml", "classpath:spring2.xml" })
Spring框架之使用JdbcTemplate开发Dao层程序
标签:nosql str tor temp 配置 life ota 属性的属性 mysql