• JdbcTemplate增删改


    1Accountsdao

    //删除单个账户
    int delaccount(Integer accountid);
    //添加单个用户
    int addaccount(Accounts accounts);
    //修改单个用户
    int updateaccount(Accounts accounts);
    代码实现

    (2)AccountdaoImpl实现类

    @Repository
    public class AccountDaoImpl implements AccountsDao {
        @Resource
        private JdbcTemplate jdbcTemplate;
     
        @Override
        public int delaccount(Integer accountid) {
    
            String sql="delete from accounts where accountid=5";
            int count = jdbcTemplate.update(sql);
            return count;
        }
    
        @Override
        public int addaccount(Accounts accounts) {
            String sql="INSERT INTO accounts(accountname,balance) VALUES (?,?)";
            int add = jdbcTemplate.update(sql,accounts.getAccountname(),accounts.getBalance());
            return add;
        }
    
        @Override
        public int updateaccount(Accounts accounts) {
            String sql="update accounts set accountname=?,balance=? where accountid=?";
            int update = jdbcTemplate.update(sql, accounts.getAccountname(), accounts.getBalance(),accounts.getAccountid());
            return update;
        }
    }
    代码实现

    3AccountService

    //删除单个账户
    int delaccount(Integer accountid);
    //添加单个用户
    int addaccount(Accounts accounts);
    //修改单个用户
    int updateaccount(Accounts accounts);
    代码实现

    4AccountServiceImpl实现类

    @Service("accountsServiceImpl")
    public class AccountsServiceImpl implements AccountsService{
        //使用Spring IOC 将AccountDao对象注入
        @Autowired
        AccountsDao dao;
    
    
        @Override
        public int delaccount(Integer accountid) {
            return dao.delaccount(accountid);
        }
    
        @Override
        public int addaccount(Accounts accounts) {
            return dao.addaccount(accounts);
        }
    
        @Override
        public int updateaccount(Accounts accounts) {
            return dao.updateaccount(accounts);
        }
    
    
        public AccountsDao getDao() {
            return dao;
        }
    
        public void setDao(AccountsDao dao) {
            this.dao = dao;
        }
    }
    代码实现

    (5)applicationContext.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:aop="http://www.springframework.org/schema/aop"
           xmlns:p="http://www.springframework.org/schema/p"
           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/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--1.配置数据源   spring内置的数据源-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="username" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>
    
        <!--2.引入属性文件-->
        <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
    
        <!--3.构建jdbcTemplate-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!--扫描注解:包扫描器-->
        <context:component-scan base-package="cn.spring"></context:component-scan>
    
        <!--开启AOP注解支持-->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    </beans>
    代码实现

    (6)测试类

    @Test
    public void delaccountTest(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountsService accountsService=(AccountsService) context.getBean(AccountsService.class);
        int delaccount = accountsService.delaccount(5);
        if (delaccount>0){
            System.out.println("删除成功!");
        }
    }
    
    @Test
    public void addaccountTest(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountsService accountsServiceImpl = (AccountsService)context.getBean("accountsServiceImpl");
        Accounts accounts=new Accounts();
        accounts.setAccountname("小丫头");
        accounts.setBalance(500);
        int addaccount = accountsServiceImpl.addaccount(accounts);
        if (addaccount>0){
            System.out.println("添加成功!");
        }
    }
    
    @Test
    public void updateaccountTest(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountsService accountsServiceImpl = (AccountsService)context.getBean("accountsServiceImpl");
        Accounts accounts=new Accounts();
        accounts.setAccountname("丫头片子");
        accounts.setBalance(1200);
        accounts.setAccountid(9);
        int updateaccount = accountsServiceImpl.updateaccount(accounts);
        if (updateaccount>0){
            System.out.println("修改成功!");
        }
    代码实现
  • 相关阅读:
    使用 ASP.NET Core MVC 创建 Web API(五)
    使用 ASP.NET Core MVC 创建 Web API(四)
    使用 ASP.NET Core MVC 创建 Web API(三)
    使用 ASP.NET Core MVC 创建 Web API(二)
    使用 ASP.NET Core MVC 创建 Web API(一)
    学习ASP.NET Core Razor 编程系列十九——分页
    学习ASP.NET Core Razor 编程系列十八——并发解决方案
    一个屌丝程序猿的人生(九十八)
    一个屌丝程序猿的人生(九十七)
    一个屌丝程序猿的人生(九十五)
  • 原文地址:https://www.cnblogs.com/Zzzzn/p/11781291.html
Copyright © 2020-2023  润新知