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

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

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

昨天用基于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;}//查询所有@Overridepublic 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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://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@Overridepublic 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;//查询所有@Overridepublic 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;}
}