Javaee Spring template实现查询数据库表内容 基于半xml半注解

文章介绍了如何将Spring应用中的Service和Dao层从基于XML的配置转换为半注解方式,以减少XML配置文件中的代码量。主要步骤包括在XML中开启注解扫描,使用@Service和@Repository注解标记Service和Dao类,并通过@Autowired注解进行依赖注入。同时,保留JdbcTemplate等Spring库的XML配置。
摘要由CSDN通过智能技术生成

昨天用基于xml配置实现template查询数据库,今天基于半xml半注解方式实现,使用注解需要导入spring-aop-5.3.8.jar

导入jar包

 

 项目结构:

 其他代码在,先前上一篇文章已经给出

AccountServiceImpl

package wwx.dao;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import wwx.domain.Account;

import java.util.List;


public class AccountDaoImpl implements AccountDao {
   //创建jdbcTemplate成员变量,及set方法
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    //查询所有
    @Override
    public List<Account> findAll() {
        System.out.println("我是Dao...");
//        JdbcTemplate jdbcTemplate=new JdbcTemplate();
//        jdbcTemplate.query();不用这种方式
        List<Account> list
                = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));

        return list;
    }
}

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">
<!--加载属性配置文件-->
    <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>

可以看到xml配置文件中非常多代码,用半xml半注解方式实现,可减少xml中代码,遵循以下原则

1.我们自己的类使用注解

2.jar包中的类使用xml

即将Spring.xml中的Servie层,Dao层在类中用注解,其余不变

四个注解:

 实现步骤

//在xml中<!--开启注解扫描-->
//<context:component-scan base-package="wwx"></context:component-scan>
//在类名上面添加@Servie注解,相当于spring.xml中的<bean id="accountService" class="wwx.service.AccountServiceImpl">
//相当于在这个容器中创建有一个AccountServiceImpl这个类的对象,这个对象的id是类名首字母小写
//如果想指定类名,格式为@Service("指定名字"),在AccountDaoImpl中,执行与这里相同步骤,不过注解为@Repository,此时已经实现配置
//再接着进行注入,使用@Autowired注解,此注解是根据类型注入的,看容器中是否有这个接口类型配合使用@Qualifier
//使用注解不需要使用set方法,可把set方法注释掉
//此时实现我们自己写的类,用注解,jar包中的类用xml,将xml中的Service层,Dao层注释掉

改后的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">
 
    <!--开启注解扫描-->
    <context:component-scan base-package="wwx"></context:component-scan>
    
    <!--加载属性配置文件-->
    <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>

    <!--JdbcTempalte层-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>
AccountServiceImpl
package wwx.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import wwx.dao.AccountDao;
import wwx.dao.AccountDaoImpl;
import wwx.domain.Account;

import java.util.List;
//在xml中<!--开启注解扫描-->
//    <context:component-scan base-package="wwx"></context:component-scan>
//在类名上面添加@Servie注解,相当于spring.xml中的<bean id="accountService" class="wwx.service.AccountServiceImpl">
//相当于在这个容器中创建有一个AccountServiceImpl这个类的对象,这个对象的id是类名首字母小写
//如果想指定类名,格式为@Service("指定名字"),在AccountDaoImpl中,执行与这里相同步骤,不过注解为@Repository,此时已经实现配置
//再接着进行注入,使用@Autowired注解,此注解是根据类型注入的,看容器中是否有这个接口类型配合使用@Qualifier
//使用注解不需要使用set方法,可把set方法注释掉
//此时实现我们自己写的类,用注解,jar包中的类用xml,将xml中的Service层,Dao层注释掉
@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired              //<property name="accountDao" ref="accountDao"></property>
    @Qualifier("accountDao")
    private AccountDao accountDao;//创建accountDao
    @Override
    public List<Account> findAll() {
        System.out.println("我是service...");
      //  AccountDao accountDao=new AccountDaoImpl();
        List<Account> list = accountDao.findAll();
        return list;
    }
}
AccountDaoImpl
package wwx.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import wwx.domain.Account;

import java.util.List;

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
   //创建jdbcTemplate成员变量,及set方法
   @Autowired
   @Qualifier("jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    //查询所有
    @Override
    public List<Account> findAll() {
        System.out.println("我是Dao...");
//        JdbcTemplate jdbcTemplate=new JdbcTemplate();
//        jdbcTemplate.query();不用这种方式
        List<Account> list
                = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));

        return list;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值