1.项目结构

2.基于xml配置的项目

<1>账户的业务层接口及其实现类

IAccountService.java

package lucky.service;

import lucky.domain.Account;

import java.util.List;

/**
* 账户的业务层接口
*/
public interface IAccountService {
/**
* 查询所有
* @return
*/
List<Account> queryAllAccount(); /**
* 查询一个
* @return
*/
Account queryAccountById(Integer accountId); /**
* 保存账户
* @param account
*/
void saveAccount(Account account); /**
* 更新账户信息
* @param account
*/
void updateAccount(Account account); /**
* 删除用户
* @param accountId
*/
void deleteAccount(Integer accountId);
}

AccountServiceImpl.java

package lucky.service.impl;

import lucky.dao.IAccountDao;
import lucky.domain.Account;
import lucky.service.IAccountService; import java.util.List; public class AccountServiceImpl implements IAccountService { private IAccountDao accountDao; public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
} public List<Account> queryAllAccount() {
return accountDao.queryAllAccount();
} public Account queryAccountById(Integer accountId) {
return accountDao.queryAccountById(accountId);
} public void saveAccount(Account account) {
accountDao.saveAccount(account);
} public void updateAccount(Account account) {
accountDao.updateAccount(account);
} public void deleteAccount(Integer accountId) {
accountDao.deleteAccount(accountId);
}
}

<2>账户的持久层接口及其实现类

IAccountDao.java

package lucky.dao;

import lucky.domain.Account;
import java.util.List; /**
* 账户的持久层接口
*/
public interface IAccountDao {
/**
* 查询所有
* @return
*/
List<Account> queryAllAccount(); /**
* 查询一个
* @return
*/
Account queryAccountById(Integer accountId); /**
* 保存账户
* @param account
*/
void saveAccount(Account account); /**
* 更新账户信息
* @param account
*/
void updateAccount(Account account); /**
* 删除用户
* @param accountId
*/
void deleteAccount(Integer accountId);
}

IAccountDaoImpl.java

package lucky.dao.impl;

import lucky.dao.IAccountDao;
import lucky.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler; import java.sql.SQLException;
import java.util.List; /**
* 账户的持久层实现类
*/
public class IAccountDaoImpl implements IAccountDao { private QueryRunner runner; public void setRunner(QueryRunner runner) {
this.runner = runner;
} public List<Account> queryAllAccount() {
try {
return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
} public Account queryAccountById(Integer accountId) {
try {
return runner.query("select * from account where id=?",new BeanHandler<Account>(Account.class),accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
} public void saveAccount(Account account) {
try {
runner.update("insert into account(name,money)values(?,?)",account.getName(),account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}
} public void updateAccount(Account account) {
try {
runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
} public void deleteAccount(Integer accountId) {
try {
runner.update("delete from account where id=?",accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}

<3>数据库表account对应的Account.java

package lucky.domain;

import java.io.Serializable;

public class Account implements Serializable {
private Integer id;
private String name;
private Float money; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Float getMoney() {
return money;
} public void setMoney(Float money) {
this.money = money;
} @Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}

<4>bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置Service -->
<!--创建AccountServiceImpl这个类的bean对象-->
<bean id="accountService" class="lucky.service.impl.AccountServiceImpl">
<!-- 使用set方法,注入dao -->
<property name="accountDao" ref="accountDao"></property>
</bean> <!--配置Dao对象-->
<bean id="accountDao" class="lucky.dao.impl.IAccountDaoImpl">
<!-- 使用set方法,注入QueryRunner -->
<property name="runner" ref="runner"></property>
</bean> <!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--使用构造函数注入,注入数据源-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean> <!-- 配置数据源 -->
<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/spring"></property>
<property name="user" value="root"></property>
<property name="password" value="plj824"></property>
</bean>
</beans>

<5>测试类

import lucky.domain.Account;
import lucky.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class AccountServiceTest {
@Test
public void testQueryAllAccount(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as= (IAccountService) ac.getBean("accountService");
//3.执行方法
List<Account> accounts = as.queryAllAccount();
for (Account account : accounts) {
System.out.println(account);
}
} @Test
public void testQueryAccountById(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as= (IAccountService) ac.getBean("accountService");
//3.执行方法
Account account1 = as.queryAccountById(1);
System.out.println(account1);
} @Test
public void testSaveAccount(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as= (IAccountService) ac.getBean("accountService");
//3.执行方法
Account account=new Account();
account.setName("lucky");
account.setMoney(1000f);
as.saveAccount(account); } @Test
public void testUpdateAccount(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as= (IAccountService) ac.getBean("accountService");
//3.执行方法
Account account1 = as.queryAccountById(1);
account1.setMoney(2000f);
as.updateAccount(account1);
} @Test
public void testDeleteAccount(){
//1.获取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as= (IAccountService) ac.getBean("accountService");
//3.执行方法
as.deleteAccount(4);
}
}

效果图:

数据库表:

testQueryAllAccount()测试输出:

testQueryAccountById()

2.基于部分注解配置的项目(一定程度上还是要使用xml配置,自己写的类用注解,jar包的类用xml配置)

<1>修改IAccountDaoImpl.java

package lucky.dao.impl;

import lucky.dao.IAccountDao;
import lucky.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import java.sql.SQLException;
import java.util.List; /**
* 账户的持久层实现类
*/
@Repository("accountDao")
public class IAccountDaoImpl implements IAccountDao {
@Autowired
private QueryRunner runner; public List<Account> queryAllAccount() {
try {
return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
} public Account queryAccountById(Integer accountId) {
try {
return runner.query("select * from account where id=?",new BeanHandler<Account>(Account.class),accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
} public void saveAccount(Account account) {
try {
runner.update("insert into account(name,money)values(?,?)",account.getName(),account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}
} public void updateAccount(Account account) {
try {
runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
} public void deleteAccount(Integer accountId) {
try {
runner.update("delete from account where id=?",accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}

<2>AccountServiceImpl.java

package lucky.service.impl;

import lucky.dao.IAccountDao;
import lucky.domain.Account;
import lucky.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service("accountService")
public class AccountServiceImpl implements IAccountService { @Autowired
private IAccountDao accountDao; public List<Account> queryAllAccount() {
return accountDao.queryAllAccount();
} public Account queryAccountById(Integer accountId) {
return accountDao.queryAccountById(accountId);
} public void saveAccount(Account account) {
accountDao.saveAccount(account);
} public void updateAccount(Account account) {
accountDao.updateAccount(account);
} public void deleteAccount(Integer accountId) {
accountDao.deleteAccount(accountId);
}
}

<3>修改bean.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
http://www.springframework.org/schema/context/spring-context.xsd"> <!--告知spring在创建容器是要扫描的包-->
<context:component-scan base-package="lucky"></context:component-scan> <!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--使用构造函数注入,注入数据源-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean> <!-- 配置数据源 -->
<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/spring"></property>
<property name="user" value="root"></property>
<property name="password" value="plj824"></property>
</bean>
</beans>

3.基于纯注解配置的项目

spring中的新注解
@Configuration
   作用:指定当前类是一个配置类
  细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。
@ ComponentScan
  作用:用于通过注解指定spring在创建容器时要扫描的包
  属性:
    value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。
    我们使用此注解就等同于在xml中配置了:
     <context:component-scan base-package="lucky"></context:component-scan>
@ Bean
  作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
  属性:
    name:用于指定bean的id。当不写时,默认值是当前方法的名称
  细节:
    当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。
    查找的方式和Autowired注解的作用是一样的
@ Import
  作用:用于导入其他的配置类
  属性:
    value:用于指定其他配置类的字节码。
  当我们使用Import的注解之后,有Import注解的类就父配置类,而导入的都是子配置类
@ PropertySource
  作用:用于指定properties文件的位置
  属性:
    value:指定文件的名称和路径。
  关键字:classpath,表示类路径下

<1>项目结构

<2>在java包下创建名为config的包

SpringConfiguration.java为主配置类

package config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource; /**
* 该类是一个配置类,它的作用和bean.xml是一样的
* spring中的新注解
* Configuration
* 作用:指定当前类是一个配置类
* 细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。
* ComponentScan
* 作用:用于通过注解指定spring在创建容器时要扫描的包
* 属性:
* value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。
* 我们使用此注解就等同于在xml中配置了:
* <context:component-scan base-package="lucky"></context:component-scan>
* Bean
* 作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中
* 属性:
* name:用于指定bean的id。当不写时,默认值是当前方法的名称
* 细节:
* 当我们使用注解配置方法时,如果方法有参数,spring框架会去容器中查找有没有可用的bean对象。
* 查找的方式和Autowired注解的作用是一样的
* Import
* 作用:用于导入其他的配置类
* 属性:
* value:用于指定其他配置类的字节码。
* 当我们使用Import的注解之后,有Import注解的类就父配置类,而导入的都是子配置类
* PropertySource
* 作用:用于指定properties文件的位置
* 属性:
* value:指定文件的名称和路径。
* 关键字:classpath,表示类路径下
*/ @Configuration
@ComponentScan(basePackages = "lucky")
@PropertySource("classpath:jdbcConfig.properties")
@Import(JdbcConfig.class)
public class SpringConfiguration { }

JdbcConfig.java

package config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope; import javax.sql.DataSource; /**
* 和spring连接数据库相关的配置类
*/
public class JdbcConfig { @Value("${jdbc.driver}")
private String driver; @Value("${jdbc.url}")
private String url; @Value("${jdbc.username}")
private String username; @Value("${jdbc.password}")
private String password; /**
* 用于创建一个QueryRunner对象
* @param dataSource
* @return
*/
@Bean(name="runner")
@Scope("prototype")
public QueryRunner createQueryRunner(@Qualifier("ds2") DataSource dataSource){
return new QueryRunner(dataSource);
} /**
* 创建数据源对象
* @return
*/
@Bean(name="ds2")
public DataSource createDataSource(){
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(username);
ds.setPassword(password);
return ds;
}catch (Exception e){
throw new RuntimeException(e);
}
} @Bean(name="ds1")
public DataSource createDataSource1(){
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(driver);
ds.setJdbcUrl("jdbc:mysql://localhost:3306/mybatis");
ds.setUser(username);
ds.setPassword(password);
return ds;
}catch (Exception e){
throw new RuntimeException(e);
}
}
}

jdbcConfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=plj824

此时,没有bean.xml文件了。

10 Spring框架--基于注解和xml的配置的应用案例的更多相关文章

  1. 10 Spring框架--基于注解的IOC配置

    1.工程环境搭建 2.基于注解的IOC配置 IOC注解的分类 (1)用于创建对象的 他们的作用就和在XML配置文件中编写一个<bean>标签实现的功能是一样的@Component: 作用: ...

  2. SpringMVC4 + Spring + MyBatis3 基于注解的最简配置

    本文使用最新版本(4.1.5)的springmvc+spring+mybatis,采用最间的配置方式来进行搭建. 1. web.xml 我们知道springmvc是基于Servlet: Dispatc ...

  3. Spring中基于注解的IOC(二):案例与总结

    2.Spring的IOC案例 创建maven项目 导入依赖 pom.xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...

  4. Spring框架——基于XML/注解开发

    IoC的实现方式有两种:XML配置文件.基于注解. MVC开发模式: Controller层 Service层 Repository层 Controller层调用Service,Service调用Re ...

  5. Spring基于注解和XML混合方式的使用

    首先要明白,基于注解和XML两种方式的实现功能是一样的,只是两种不同的配置方式. 一.IoC配置 1.配置xml 在使用注解与xml结合的方式配置IoC之前,首先要引入context标签: xmlns ...

  6. spring mvc 基于注解的使用总结

    本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...

  7. Spring:基于注解的Spring MVC

    什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...

  8. Spring+MyBatis纯注解零XML整合(4)

    不得不说,利用XML作为配置文件是一个非常好的想法,它可以轻松地实现配置集中化,而且修改之后无需再次编译.然而,由于大多数情况下开发者基本都会拿到程序的源码,加之对于各种XML配置文件一般情况下也只有 ...

  9. 10.Spring——框架的AOP

    1.Spring 框架的 AOP 2.Spring 中基于 AOP 的 XML架构 3.Spring 中基于 AOP 的 @AspectJ 1.Spring 框架的 AOP Spring 框架的一个关 ...

随机推荐

  1. 【笔记】ROS常用命令

    环境相关 查看当前环境下包含的包路径echo $ROS_PACKAGE_PATH查看包含的包的路径roscd package TF树相关 查看所有坐标系的状态rosrun tf tf_monitor ...

  2. JavaScript基础07——BOM

    BOM概念   BOM是Browser Object Model的缩写,简称浏览器对象模型.这个对象就是window         BOM提供了独立于内容而与浏览器窗口进行交互的对象         ...

  3. cube.js 学习 cube 连接mongodb 试用

    cube.js 对于mongodb 的连接是通过mongodb bi connector(mysql 协议)处理的,以下为简单的试用 安装mongo bi connector 这个玩意用docker ...

  4. 75: libreoj #10028 双向宽搜

    $des$ 实现一个bfs $sol$ 写了一个双向bfs #include <bits/stdc++.h> using namespace std; #define Rep(i, a, ...

  5. sqlalchemy lock and atomic

    prepare: MYSQL tutorial Prepare a table set evn DBUSER=root DBPASS= DBNAME=cyborgTBNAME="atomic ...

  6. 【luoguP2371】 [国家集训队]墨墨的等式

    题目链接 考虑将所有的\(a_1x_1+a_2x_2+--+a_nx_n=B\)对\(a_1\)取模,那么所有可达到的B就分为了\(0\)~\(a_1-1\)类 如果对\(a_1\)取模为\(k\)的 ...

  7. springboot2.0整合redis作为缓存以json格式存储对象

    步骤1 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spr ...

  8. 【18NOIP普及组】对称二叉树(信息学奥赛一本通 1981)(洛谷 5018)

    [题目描述] 一棵有点权的有根树如果满足以下条件,则被轩轩称为对称二叉树: 1.二叉树: 2.将这棵树所有节点的左右子树交换,新树和原树对应位置的结构相同且点权相等. 下图中节点内的数字为权值,节点外 ...

  9. vuejs cli3 env配置文件实践指南

    env文件 配置四个环境名字分别 VUE_APP_ENV=dev 只支持:VUE_APP_ 开头,比如设置其它变量 VUE_APP_NAME=stark package.json 配置 方式 &quo ...

  10. 菜鸟学IT之豆瓣爬取初体验

    作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3159 可以用pandas读出之前保存的数据: newsdf = pd.re ...