遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

Spring注解综合应用

需求

通过注解方式,实现下面xml的配置(实现“控制层(controller)--业务逻辑层(service)--dao层--数据源”的关系)

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  
    <!--可以配置多个数据源,用哪个就引用哪个-->
    <!--mysql-->
    <bean id="ds" class="com.qzcsbj.dao.impl.DataSource">
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
        <property name="url" value="jdbc:mysql://192.168.168.168:3360/gifts?useUnicode=true&characterEncoding=utf-8&useSSL=true"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    </bean>
  
    <!--dao引用ds-->
    <bean id="dao" class="com.qzcsbj.dao.impl.UserDaoImpl">
        <property name="ds" ref="ds"/>
    </bean>
  
    <!--service引用dao-->
    <bean id="service" class="com.qzcsbj.service.impl.UserServiceImpl">
        <property name="userDao" ref="dao"/>
    </bean>
  
    <!--controller引用service-->
    <bean id="controller" class="com.qzcsbj.controller.UserController">
        <property name="userService" ref="service"/>
    </bean>
</beans>

pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qzcsbj.myspring</groupId>
    <artifactId>myspring</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.version>4.3.14.RELEASE</spring.version>
        <!--<spring.version>4.3.7.RELEASE</spring.version>-->
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <dependencies>
        <!-- spring需要的jar包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework</groupId>-->
            <!--<artifactId>spring-context-support</artifactId>-->
            <!--<version>${spring.version}</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--日志-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <!--Spring测试模块-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

 

实体类

package com.qzcsbj.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@Component
public class User {
    @Value("jack")
    private String name;

    @Value("男")
    private String sex;

    public User() {
    }

    public User(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

  

DataSource

package com.qzcsbj.dao.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@Component
public class DataSource {
    @Value("root")
    private String username;
    @Value("123456")
    private String password;
    @Value("jdbc:mysql://192.168.168.168:3360/gifts?useUnicode=true&characterEncoding=utf-8&useSSL=true")
    private String url;
    @Value("com.mysql.jdbc.Driver")
    private String driverClass;

    @Override
    public String toString() {
        return "DataSource{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", url='" + url + '\'' +
                ", driverClass='" + driverClass + '\'' +
                '}';
    }
}

 

dao层接口

package com.qzcsbj.dao;

import com.qzcsbj.bean.User;
import java.util.List;

/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
public interface UserDao {
    List<User> getUsers();
}

dao层实现类

说明:mybatis不需要实现类,Mybatis的dao层是自动生成对象的

package com.qzcsbj.dao.impl;

import com.qzcsbj.bean.User;
import com.qzcsbj.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@Repository
// @Repository("dao")  // 如果给对象取名了,那么service层注入的时候需要写对象名
public class UserDaoImpl implements UserDao {
    @Autowired  // 因为只有一个DataSource对象,所以也可以用@Resource
    private DataSource ds;  // 可以改为私有
    // DataSource ds = null;
    // DataSource ds;  // 或者写成这样也行,因为运行过程中会给他赋值

    public List<User> getUsers() {
        System.out.println("===========连接的数据库是:" + ds);
        User u1 = new User("jack","男");
        User u2 = new User("lucy","女");
        List<User> list = new ArrayList<User>();  // 这里是模拟查库获取数据
        list.add(u1);
        list.add(u2);
        return list;
    }
}

  

service层接口

package com.qzcsbj.service;

import com.qzcsbj.bean.User;
import java.util.List;

/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
public interface UserService {
    // 接口和dao层差不多
    List<User> getUsers();
}

  

service层实现类

package com.qzcsbj.service.impl;

import com.qzcsbj.bean.User;
import com.qzcsbj.dao.UserDao;
import com.qzcsbj.dao.impl.UserDaoImpl;
import com.qzcsbj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    // @Resource(name = "dao")  // 如果dao指定了对象名称
    UserDao userDao = null;

    public List<User> getUsers() {
        return userDao.getUsers();
    }
}

  

controller层

package com.qzcsbj.controller;

import com.qzcsbj.bean.User;
import com.qzcsbj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;  // 可以定义为私有

    public void getUser(){
        List<User> users = userService.getUsers();
        for (User user : users) {
            System.out.println(user);
        }
    }
}

 

创建配置文件: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: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="com.qzcsbj.*"/>

</beans>

 

测试

package com.qzcsbj.test;

import com.qzcsbj.controller.UserController;
import org.apache.log4j.Logger;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;

/**
 * @公众号 : 全栈测试笔记
 * @博客 : www.cnblogs.com/uncleyong
 * @微信 : ren168632201
 * @描述 : <>
 */
@RunWith(SpringJUnit4ClassRunner.class)  // 表示Spring和JUnit整合测试
@ContextConfiguration("classpath:applicationContext.xml")  // 指定创建容器时使用哪个配置文件
public class Test {
    // @Autowired  // 对象只有一个,所以这里直接写@Resource也可以
    @Resource
    UserController userController;

    @org.junit.Test
    public void test(){
        userController.getUser();
    }

}

  

结果

 

【bak】

原文会持续更新,原文地址:https://www.cnblogs.com/uncleyong/p/17020196.html

 

posted @ 2023-01-02 20:36  全栈测试笔记  阅读(221)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end