您现在的位置是:首页 > 编程 > 

【旧】G004Spring学习笔记

2025-07-26 15:35:42
【旧】G004Spring学习笔记 一、XML方式实现1、数据库创建语句代码语言:javascript代码运行次数:0运行复制create table account( id int primary key auto_increment, name varchar(40), money float )character set utf8 collate utf8_general_ci; i

【旧】G004Spring学习笔记

一、XML方式实现

1、数据库创建语句

代码语言:javascript代码运行次数:0运行复制
create table account(
	id int primary key auto_increment,
	name varchar(40),
	money float
)character set utf8 collate utf8_general_ci;

insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);

2、pom.xml文件

代码语言:javascript代码运行次数:0运行复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0"
         xmlns:xsi=";
         xsi:schemaLocation=".0.0 .0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId></groupId>
    <artifactId>spring05</artifactId>
    <version>1.0-SAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>
        <dependency>
            <groupId>comm-dbutils</groupId>
            <artifactId>comm-dbutils</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>cp0</groupId>
            <artifactId>cp0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.1-beta-</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

、bean.xml文件

代码语言:javascript代码运行次数:0运行复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=";
       xmlns:xsi=";
       xsi:schemaLocation="
        .xsd">

    <!--配置Service-->
    <bean id="accountService" class="com.zibo.service.impl.AccountServiceImpl">
        <!--注入dao-->
        <property name="accountDao" ref="accountDao"/>
    </bean>
    <!--配置Dao-->
    <bean id="accountDao" class="com.zibo.dao.impl.AccountDaoImpl">
        <!--注入QueryRunner-->
        <property name="runner" ref="runner"/>
    </bean>
    <!--配置QueryRunner,多例-->
    <bean id="runner" class="org.apachem.dbutils.QueryRunner" scope="prototype">
        <!--注入数据源-->
        <ctructor-arg name="ds" ref="dataSource"/>
    </bean>
    <!--配置数据源-->
    <bean id="dataSource" class="ComboPooledDataSource">
        <!--注入连接数据库的信息-->
        <property name="driverClass" value="jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:06/zibo?serverTimezone=UTC"/>
        <property name="user" value="***************"/>
        <property name="password" value="***************"/>
    </bean>
</beans>

4、接口IAccountDao

代码语言:javascript代码运行次数:0运行复制
package com.zibo.dao;

import com.zibo.domain.Account;

import java.util.List;

public interface IAccountDao {
    //查询所有账户
    List<Account> findAllAccount();
    //根据id查询账户
    Account findAccountById(Integer accountId);
    //保存账户
    void saveAccount(Account account);
    //更新账户
    void updateAccount(Account account);
    //删除用户
    void deleteAccountById(Integer accountId);
}

5、接口实现类AccountDaoImpl

代码语言:javascript代码运行次数:0运行复制
package com.zibo.dao.impl;

import com.zibo.dao.IAccountDao;
import com.zibo.domain.Account;
import org.apachem.dbutils.QueryRunner;
import org.apachem.dbutils.handlers.BeanHandler;
import org.apachem.dbutils.handlers.BeanListHandler;

import java.util.List;

/**
 * 账户的持久层实现类
 */

public class AccountDaoImpl implements IAccountDao {

    private QueryRunner runner;

    public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }

    @Override
    public List<Account> findAllAccount() {
        try {
            return runner.query("select * from account",new BeanListHandler<>());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public Account findAccountById(Integer accountId) {
        try {
            return runner.query("select * from account where id = ?",new BeanHandler<>(),accountId);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void saveAccount(Account account) {
        try {
            runner.update("insert into account(name,money) values(?,?)",account.getame(),account.getMoney());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateAccount(Account account) {
        try {
            runner.update("update account set name = ?, money = ? where id = ?",account.getame(),account.getMoney(),account.getId());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteAccountById(Integer accountId) {
        try {
            runner.update("delete from account where id = ?",accountId);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}

6、接口IAccountService

代码语言:javascript代码运行次数:0运行复制
package com.zibo.service;

import com.zibo.domain.Account;

import java.util.List;

/**
 *  账户的业务层接口
 */
public interface IAccountService {
    //查询所有账户
    List<Account> findAllAccount();
    //根据id查询账户
    Account findAccountById(Integer accountId);
    //保存账户
    void saveAccount(Account account);
    //更新账户
    void updateAccount(Account account);
    //删除用户
    void deleteAccountById(Integer accountId);
}

7、接口实现类AccountServiceImpl

代码语言:javascript代码运行次数:0运行复制
package com.zibo.service.impl;

import com.zibo.dao.IAccountDao;
import com.zibo.domain.Account;
import com.zibo.service.IAccountService;

import java.util.List;

public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao;

    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }

    @Override
    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);
    }

    @Override
    public void saveAccount(Account account) {
        accountDao.saveAccount(account);
    }

    @Override
    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }

    @Override
    public void deleteAccountById(Integer accountId) {
        accountDao.deleteAccountById(accountId);
    }
}

8、实体类Account

代码语言:javascript代码运行次数:0运行复制
package com.zibo.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 getame() {
        return name;
    }

    public void setame(String name) {
         = name;
    }

    public float getMoney() {
        return money;
    }

    public void setMoney(float money) {
         = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

9、测试类AccountServiceTest

代码语言:javascript代码运行次数:0运行复制
package com.;

import com.zibo.domain.Account;
import com.zibo.service.IAccountService;
import org.junit.Before;
import org.junit.Test;
import org.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * 使用junit单元测试进行测试
 */
public class AccountServiceTest {
    private ClassPathXmlApplicationContext ac;
    private IAccountService as;
    @Before
    public void init(){
        //1、获取容器
        ac = new ClassPathXmlApplicationContext("bean.xml");
        //2、得到业务层对象
        as = ac.getBean("accountService",);
    }
    @Before
    public void end(){
        ();
    }
    @Test
    public void testFindAllAccount(){
        //、执行方法
        List<Account> accounts = as.findAllAccount();
        //4、遍历输出
        for (Account account : accounts) {
            println(account);
        }
    }
    @Test
    public void testFindAccountById(){
        Account account = as.findAccountById(1);
        println(account);
    }
    @Test
    public void testSave(){
        Account account = new Account();
        account.setame("大哥");
        account.setMoney(2000);
        as.saveAccount(account);
    }
    @Test
    public void testUpdate(){
        Account account = new Account();
        account.setId();
        account.setame("二哥");
        account.setMoney(000);
        as.updateAccount(account);
    }
    @Test
    public void testDelete(){
        as.deleteAccountById(1);
    }
}
二、注解方式实现

1、说明

目前的注解方式只需要对XML稍作修改,下面把更改的代码贴出来,其余代码见上面;

2、代码

bean.xml文件:
代码语言:javascript代码运行次数:0运行复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=";
       xmlns:xsi=";
       xmlns:context=";
       xsi:schemaLocation="
        .xsd
        
        .xsd">
    <!--告诉spring要创建容器时要扫描的包,但是配置所需要的标签不在<beans/>标签中,
        而是一个名称为context的名称空间和约束中-->
    <context:component-scan base-package="com.zibo"/>
    <!--配置QueryRunner,多例-->
    <bean id="runner" class="org.apachem.dbutils.QueryRunner" scope="prototype">
        <!--注入数据源-->
        <ctructor-arg name="ds" ref="dataSource"/>
    </bean>
    <!--配置数据源-->
    <bean id="dataSource" class="ComboPooledDataSource">
        <!--注入连接数据库的信息-->
        <property name="driverClass" value="jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:06/zibo?serverTimezone=UTC"/>
        <property name="user" value="***************"/>
        <property name="password" value="***************"/>
    </bean>
</beans>
接口实现类AccountServiceImpl:
代码语言:javascript代码运行次数:0运行复制
package com.zibo.service.impl;

import com.zibo.dao.IAccountDao;
import com.zibo.domain.Account;
import com.zibo.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;

    @Override
    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }

    @Override
    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);
    }

    @Override
    public void saveAccount(Account account) {
        accountDao.saveAccount(account);
    }

    @Override
    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }

    @Override
    public void deleteAccountById(Integer accountId) {
        accountDao.deleteAccountById(accountId);
    }
}
接口实现类AccountDaoImpl:
代码语言:javascript代码运行次数:0运行复制
package com.zibo.dao.impl;

import com.zibo.dao.IAccountDao;
import com.zibo.domain.Account;
import org.apachem.dbutils.QueryRunner;
import org.apachem.dbutils.handlers.BeanHandler;
import org.apachem.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 账户的持久层实现类
 */
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
    @Autowired
    private QueryRunner runner;

    @Override
    public List<Account> findAllAccount() {
        try {
            return runner.query("select * from account",new BeanListHandler<>());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public Account findAccountById(Integer accountId) {
        try {
            return runner.query("select * from account where id = ?",new BeanHandler<>(),accountId);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void saveAccount(Account account) {
        try {
            runner.update("insert into account(name,money) values(?,?)",account.getame(),account.getMoney());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateAccount(Account account) {
        try {
            runner.update("update account set name = ?, money = ? where id = ?",account.getame(),account.getMoney(),account.getId());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteAccountById(Integer accountId) {
        try {
            runner.update("delete from account where id = ?",accountId);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent 删除配置学习笔记accountioc接口

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1199199.html

相关标签:无
上传时间: 2025-07-23 14:35:01
留言与评论(共有 9 条评论)
本站网友 平湖秋月
9分钟前 发表
而是一个名称为context的名称空间和约束中--> <context
本站网友 民生人寿保险
8分钟前 发表
money = ? where id = ?"
本站网友 swisse蔓越莓
21分钟前 发表
1000);2
本站网友 沈阳妇科
12分钟前 发表
new BeanHandler<>()
本站网友 l163
30分钟前 发表
new BeanListHandler<>()); }catch (Exception e){ throw new RuntimeException(e); } } @Override public Account findAccountById(Integer accountId) { try { return runner.query("select * from account where id = ?"
本站网友 北京住房公积金网站
27分钟前 发表
schemaLocation=" .xsd"> <!--配置Service--> <bean id="accountService" class="com.zibo.service.impl.AccountServiceImpl"> <!--注入dao--> <property name="accountDao" ref="accountDao"/> </bean> <!--配置Dao--> <bean id="accountDao" class="com.zibo.dao.impl.AccountDaoImpl"> <!--注入QueryRunner--> <property name="runner" ref="runner"/> </bean> <!--配置QueryRunner
本站网友 方莉
27分钟前 发表
account.getMoney()); }catch (Exception e){ throw new RuntimeException(e); } } @Override public void updateAccount(Account account) { try { runner.update("update account set name = ?
本站网友 重庆电动门
18分钟前 发表
account.getMoney()