欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring框架纯注解开发

程序员文章站 2022-07-12 23:02:25
...

编写三个测试类

package com.itheima.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

/**
 * @Configuration
 * 表示该类是一个Spring的配置类,用来存放spring的配置信息
 * @ComponentScan
 * 用于指定 Spring 在初始化容器时要扫描的包,相当于xml配置时的<context:component-scan base-package="com.itheima"/>
 * @PropertySource
 * 用于加载配置文件,相当于xml配置时的<context:property-placeholder location="classpath:jdbc.properties"/>
 * @Import
 * 用于导入其他的配置类,相当于xml配置时的<import resource="classpath:applicationContext-dao.xml"/>
 */
@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")    //
@Import({JDBCConfig.class, MybatisConfig.class})    //用于导入其他配置类
public class SpringConfig {
}
package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MybatisConfig {

    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.domain");
        return sqlSessionFactoryBean;
    }

    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.itheima.dao");
        return mapperScannerConfigurer;
    }
}
package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

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;

    @Bean("dataSource")
    public DataSource getDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}

数据访问层接口

package com.itheima.dao;

import com.itheima.domain.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UserDao {

    @Insert("INSERT INTO user VALUES (#{id},#{user_id},#{username},#{age},#{sex},#{phone});")
    int save(User user);

    @Update("UPDATE user SET username=#{username}, age=#{age}, sex=#{sex}, phone=#{phone} WHERE user_id=#{user_id}")
    int update(User user);

    @Delete("DELETE FROM user WHERE user_id=#{user_id}")
    int delete(String user_id);

    @Select("SELECT * FROM user WHERE user_id = #{user_id}")
    User findById(String user_id);

    @Select("SELECT * FROM user")
    List<User> findAll();
}

业务逻辑层接口及实现类

package com.itheima.service;

import com.itheima.domain.User;

import java.util.List;

public interface UserService {

    int save(User user);

    int update(User user);

    int delete(String user_id);

    User findById(String user_id);

    List<User> findAll();
}

//==========实现类分割线==================

package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public int save(User user) {
        return userDao.save(user);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public int delete(String user_id) {
        return userDao.delete(user_id);
    }

    @Override
    public User findById(String user_id) {
        return userDao.findById(user_id);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }
}

编写测试类有两种办法:
一:
需要导入包

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.1.9.RELEASE</version>
</dependency>
package com.itheima;

import com.itheima.config.SpringConfig;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class App {

    @Autowired
    private UserService userService;

    @Test
    public void test(){
        User user = userService.findById("1001");
        System.out.println(user);
    }
}

二:
原始方法

package com.itheima;

import com.itheima.config.SpringConfig;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {

    @Test
    public void test(){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = (UserService) applicationContext.getBean(UserServiceImpl.class);
        User user = userService.findById("1001");
        System.out.println(user);
    }
}