系统开发过程中,自定义的类可以用注解装配Bean对象,而第三方类用xml装配Bean对象。

添加依赖POM.xml

<?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.company</groupId>
<artifactId>Spring_annotation01</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <target>1.8</target>
                <source>1.8</source>
                <encoding>utf-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>7.2.2.jre8</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-dbutils</groupId>
        <artifactId>commons-dbutils</artifactId>
        <version>1.7</version>
    </dependency>
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.4</version>
    </dependency>
</dependencies>
</project>

POJO目录下的Account.java

public class Account {
    private int id;
    private String name;
    private float money;

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public float getMoney() {
        return money;
    }

    public void setMoney(float money) {
        this.money = money;
    }
}

Spring配置文件 applicationContext.xml(注意添加xml context约束)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.company"></context:component-scan>
    <!--裝配Bean對象 c3p0數據源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
        <property name="jdbcUrl" value="jdbc:sqlserver://localhost:1433;DatabaseName=testdb"></property>
        <property name="user" value="sa"></property>
        <property name="password" value="123.abc"></property>
    </bean>
    <!--裝配Bean對象 配置QueryRunner數據源-->
    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
        <constructor-arg name="ds" ref="dataSource"></constructor-arg>
    </bean>
</beans>

dao目录

AccountDao.java

public interface AccountDao {
    void saveAccount(Account account) throws SQLException;
    void updateAccount(Account account) throws SQLException;
    void deleteAccountById(int id) throws SQLException;
    Account queryAccountById(int id) throws SQLException;
    List<Account> queryAccountList() throws SQLException;
}

AccountDaoImpl.java

//@Repository裝配Bean對象
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
    //使用註解依賴注入
    @Autowired
    @Qualifier("queryRunner")
    private QueryRunner queryRunner;

    @Override
    public void saveAccount(Account account) throws SQLException {
        String sql="insert into account(name,money) values(?,?)";
        Object[] param={account.getName(),account.getMoney()};
        queryRunner.update(sql,param);
    }

    @Override
    public void updateAccount(Account account) throws SQLException {
        String sql="update account set name=?, money=? where id=?";
        Object[] param={account.getName(),account.getMoney(),account.getId()};
        queryRunner.update(sql,param);
    }

    @Override
    public void deleteAccountById(int id) throws SQLException {
        String sql="delete from account where id=?";
        queryRunner.update(sql,id);
    }

    @Override
    public Account queryAccountById(int id) throws SQLException {
        String sql="select * from account where id=?";
        Account account = queryRunner.query(sql, new BeanHandler<>(Account.class), id);
        return  account;
    }

    @Override
    public List<Account> queryAccountList() throws SQLException {
        String sql="select * from account";
        List<Account> accountList = queryRunner.query(sql, new BeanListHandler<>(Account.class));
        return accountList;
    }
}

service目录下

AccountService.java

public interface AccountService {
    void saveAccount(Account account);
    void updateAccount(Account account);
    void deleteAccountById(int id);
    Account queryAccountById(int id);
    List<Account> queryAccountList();
}

AccountServiceImpl.java

//@Service註解裝配Bean對象
@Service("accountService")
@Scope("prototype")
public class AccountServiceImpl implements AccountService {
    //使用註解依賴注入
    @Autowired
    @Qualifier("accountDao")
    //@Resource(name = "accountDao")
    private AccountDao accountDao;

    @Override
    public void saveAccount(Account account) {
        try {
            accountDao.saveAccount(account);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateAccount(Account account) {
        try {
            accountDao.updateAccount(account);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteAccountById(int id) {
        try {
            accountDao.deleteAccountById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Account queryAccountById(int id) {
        Account account=null;
        try {
            account=accountDao.queryAccountById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return account;
    }

    @Override
    public List<Account> queryAccountList() {
        List<Account> accountList = null;
        try {
            accountList=accountDao.queryAccountList();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return accountList;
    }
}

测试

public class AnnotationTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        AccountService accountService1 = applicationContext.getBean("accountService", AccountService.class);
        AccountDao accountDao = applicationContext.getBean("accountDao", AccountDao.class);
        System.out.println(accountService);
        System.out.println(accountService1);
        System.out.println(accountDao);
    }

    @Test
    public void saveAccountTest(){
       ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        Account account = new Account();
        account.setName("november");
        account.setMoney(3000f);
        accountService.saveAccount(account);
    }

    @Test
    public void updateAccountTest(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        Account account = new Account();
        account.setId(7);
        account.setName("june");
        account.setMoney(7000f);
        accountService.updateAccount(account);
    }
    @Test
    public void deleteAccountById(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        accountService.deleteAccountById(8);
    }
    @Test
    public void queryAccountById(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        Account account = accountService.queryAccountById(3);
        System.out.println(account);
    }
    @Test
    public void queryAccountList(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        List<Account> accountList = accountService.queryAccountList();
        accountList.forEach(System.out::println);
    }
}

 

 posted on 2019-10-25 14:56  会飞的金鱼  阅读(141)  评论(0)    收藏  举报