Spring之注解查询数据库

User实体类

public class User implements Serializable {
    private Integer id;
    private String userName;
    private Date birthday;
    private String sex;
    private String address;

AccountDaoImpl

@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {

	@Autowired
    private QueryRunner runner;

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

AccountServiceImpl

@Service("accountService")
public class AccountServiceImpl implements IAccountService {

	@Autowired
    public IAccountDao accountDao;

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

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">
    <context:component-scan base-package="net.togogo"></context:component-scan>
    <!--配置runner-->
    <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/eesy"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

</beans>

完全不用xml

1.@Configuration

作用:指定当前类是一个配置类(此类作用:和applicationContext.xml的作用时一样的)

2.@ComponentScan

作用:用于通过注解指定spring在创建容器时要扫描的包
属性(value):它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。
使用此注解等于在xml中配置了:

<context:component-scan base-package="net.togogo"></context:component-scan>

3.@Bean
作用:用于把当前方法的返回值作为bean对象存入spring的Ioc容器中。
属性name:用于指定bean的id。当不写时,默认时当前方法的名称。
细节:当我们使用注解配置方法时,如果有方法参数,spring框架会去容器查找有没有可用的bean对象。 查找的方式和Autowired注解的作用是一样的。

/**
     * 用于创建一个QueryRunner对象
     * @param dataSource
     * @return
     */
    @Bean(name = "runner")
    public QueryRunner createQueryRunner(DataSource dataSource){
        return new QueryRunner(dataSource);
    }

测试类中:获取容器要用AnnotationConfigApplicationContext

 //1.获取容器
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);

此时的QueryRunner对象是单例的,于是需要在创建QueryRunner的类上加上@Scope("prototype")

注意

当我们在测试类中用AnnotationConfigApplicationContext时,参数指定了配置类,则可以不写 @Configuration

当有另外一个配置类的时候,我们要在该配置类上写 @Configuration ,而且要在主配置类上对该配置类的包进行扫描(@ComponentScan

当只有一个配置类时,AnnotationConfigApplicationContext内必须有参数指定它。

还可以:设置父子配置类
4.@import

  • 作用:用于导入其他的配置类。
  • 当我们使用Import的注解之后,有Import注解的类是父配置类,而导入的都是子配置类
  • 属性value:用于指定其他配置类字节码。

可以采取配置文件得方法把连接数据库得几个参数放出来
jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy
jdbc.username=root
jdbc.password=123456

JdbcConfig类中:

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;

5.PropertySource

  • 作用:用于指定properties文件的位置
  • 属性value:指定文件的名称和路径。
  • 关键字:classpath,表示类路径下

在主配置类中:加上@PropertySource("classpath:jdbc.properties")

发布了31 篇原创文章 · 获赞 1 · 访问量 255

猜你喜欢

转载自blog.csdn.net/weixin_41605945/article/details/104460780