Spring--基于IOC的CRUD操作,springmvc面试题

创建数据库表  

导入相关依赖  

创建账户实体类  

配置表现层与持久层  

通过XML或注解实现CRUD操作  

测试代码

创建相关的数据库表:


create database spring;

use spring;

Drop table if exists account;

create table account(

id int primary key auto_increment,

name varchar(40),

money float

)character set utf8 collate utf8_general_ci;



insert into account (name,money) values('aaa',1000);

insert into account (name,money) values('bbb',1000);

insert into account (name,money) values('ccc',1000); 

数据库表:

Spring--基于IOC的CRUD操作,springmvc面试题

基于XML的IOC案例


项目结构:

Spring--基于IOC的CRUD操作,springmvc面试题

在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>org.example</groupId>

    <artifactId>spring_day02_account_xml</artifactId>

    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>



    <dependencies>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-context</artifactId>

            <version>5.0.2.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>4.11</version>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>commons-dbutils</groupId>

            <artifactId>commons-dbutils</artifactId>

            <version>1.4</version>

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>8.0.11</version>

        </dependency>

        <dependency>

            <groupId>c3p0</groupId>

            <artifactId>c3p0</artifactId>

            <version>0.9.1.2</version>

        </dependency>

    </dependencies>



</project> 

创建account实体类:


/**

 * @Author: Ly

 * @Date: 2020-07-20 18:55

 */

public class Account implements Serializable {



    private Integer id;

    private String name;

    private Float money;



    public Integer getId() {

        return id;

    }



    public void setId(Integer 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;

    }



    @Override

    public String toString() {

        return "Account{" +

                "id=" + id +

                ", name='" + name + '\'' +

                ", money=" + money +

                '}';

    }

} 

创建并实现业务层接口:


/**

 * @Author: Ly

 * @Date: 2020-07-21 16:29

 * 账户业务层的接口

 */

public interface IAccountService {

    /**

     * 查询所有

     * @return

     */

    List<Account> findAllAccount();



    /**

     * 查询一个

     * @param accountId

     * @return

     */

    Account findAccountById(Integer accountId);



    /**

     * 保存

     * @param account

     */

    void saveAccount(Account account);



    /**

     * 更新

     * @param account

     */

    void updateAccount(Account account);



    /**

     * 删除

     * @param accountId

     */

    void deleteAccount(Integer accountId);

} 

/**

  • @Author: Ly

  • @Date: 2020-07-21 16:31

  • 账户的业务成实现类

*/

public class AccountServiceImpl implements IAccountService {

private IAccountDao accountDao;



public void setAccountDao(IAccountDao accountDao) {

    this.accountDao = accountDao;

}



public List<Account> findAllAccount() {

    return accountDao.findAllAccount();

}



public Account findAccountById(Integer accountId) {

    return accountDao.findAccountById(accountId);

}



public void saveAccount(Account account) {

    accountDao.saveAccount(account);

}



public void updateAccount(Account account) {

    accountDao.updateAccount(account);

}



public void deleteAccount(Integer accountId) {

    accountDao.deleteAccount(accountId);

}

}




创建并实现持久层接口:



/**

  • @Author: Ly

  • @Date: 2020-07-21 16:33

  • 账户的持久层接口

*/

public interface IAccountDao {

/**

 * 查询所有

 * @return

 */

List<Account> findAllAccount();



/**

 * 查询一个

 * @param accountId

 * @return

 */

Account findAccountById(Integer accountId);



/**

 * 保存

 * @param account

 */

void saveAccount(Account account);



/**

 * 更新

 * @param account

 */

void updateAccount(Account account);



/**

 * 删除

 * @param accountId

 */

void deleteAccount(Integer accountId);

}


/**

 * 账户的持久层实现类

 */

public class AccountDaoImpl implements IAccountDao {



    private QueryRunner runner;



    public void setRunner(QueryRunner runner) {

        this.runner = runner;

    }



    public List<Account> findAllAccount() {

        try {

            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }



    public Account findAccountById(Integer accountId) {

        try {

            return runner.query("select * from account where id= ?",new BeanHandler<Account>(Account.class),accountId);

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }



    public void saveAccount(Account account) {

        try {

            runner.update("insert into account(name,money) values(?,?)",account.getName(),account.getId());

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }



    public void updateAccount(Account account) {

        try {

            runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }



    public void deleteAccount(Integer accountId) {

        try {

            runner.update("delete from account where id=?",accountId);

        } catch (SQLException e) {

            throw new RuntimeException(e);

        }

    }

} 

```



配置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"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans.xsd">



    <!--配置业务层对象Service-->

   <bean id="accountService" class="com.ly.service.impl.AccountServiceImpl">

       <!--注入dao对象-->

       <property name="accountDao" ref="accountDao"></property>

   </bean>



    <!--配置Data对象-->

    <bean id="accountDao" class="com.ly.dao.impl.AccountDaoImpl">

        <!--注入QueryRunner-->

        <property name="runner" ref="runner"></property>

    </bean>



    <!--配置QueryRunner-->

    <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.cj.jdbc.Driver"></property>



# 最后

**[CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】](https://codechina.csdn.net/m0_60958482/java-p7)**

![](https://www.icode9.com/i/ll/?i=img_convert/71286461e55fb91d5a4fb2b208a69003.png)

![](https://www.icode9.com/i/ll/?i=img_convert/801bde00a1f20b10252e5a8800f1960b.png)

    </bean>



    <!--配置QueryRunner-->

    <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.cj.jdbc.Driver"></property>



# 最后

**[CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】](https://codechina.csdn.net/m0_60958482/java-p7)**

[外链图片转存中...(img-bX4CT5n3-1630841045379)]

[外链图片转存中...(img-h2h6S4qW-1630841045380)]

![](https://www.icode9.com/i/ll/?i=img_convert/75d03bc41b86865b384048958c2070a0.png)
上一篇:Java生成和操作Excel文件


下一篇:mysql建表语句