spring development case with mysql

Entity classes:

 1 package cn.mepu.domain;
 2 
 3 import java.io.Serializable;
 4 
 5 /**
 6  * 账户实体类
 7  * @author shkstart
 8  * @create 2019-11-08 10:04
 9  */
10 public class Account implements Serializable {
11     private Integer id;
12     private String name;
13     private float money;
14 
15     public Account(Integer id, String name, float money) {
16         this.id = id;
17         this.name = name;
18         this.money = money;
19     }
20 
21     public Account() {
22     }
23 
24     public Integer getId() {
25         return id;
26     }
27 
28     public void setId(Integer id) {
29         this.id = id;
30     }
31 
32     public String getName() {
33         return name;
34     }
35 
36     public void setName(String name) {
37         this.name = name;
38     }
39 
40     public float getMoney() {
41         return money;
42     }
43 
44     public void setMoney(float money) {
45         this.money = money;
46     }
47 
48     @Override
49     public String toString() {
50         return "Account{" +
51                 "id=" + id +
52                 ", name='" + name + '\'' +
53                 ", money=" + money +
54                 '}';
55     }
56 }

 

dao layer:

 1 package cn.mepu.dao.imp;
 2 
 3 import cn.mepu.dao.AccountDao;
 4 import cn.mepu.domain.Account;
 5 import org.apache.commons.dbutils.QueryRunner;
 6 import org.apache.commons.dbutils.handlers.BeanHandler;
 7 import org.apache.commons.dbutils.handlers.BeanListHandler;
 8 
 9 import java.util.List;
10 
11 /**
12  * @author shkstart
13  * @create 2019-11-08 10:28
14  */
15 public class AccountDaoImp implements AccountDao {
16 
17     private QueryRunner runner;
18 
19     public void setRunner(QueryRunner runner) {
20         this.runner = runner;
21     }
22 
23     @Override
24     public List<Account> findAllAccount() {
25         try {
26             return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
27         } catch (Exception e) {
28             throw new RuntimeException(e);
29         }
30     }
31 
32     @Override
33     public Account findAccountById(Integer accountId) {
34         try {
35             return runner.query("select * from account where id = ? ",new BeanHandler<Account>(Account.class),accountId);
36         } catch (Exception e) {
37             throw new RuntimeException(e);
38         }
39     }
40 
41     @Override
42     public void saveAccount(Account acc) {
43         try {
44             runner.update("insert into account(name,money) values(?,?)" , acc.getName(),acc.getMoney());
45         } catch (Exception e) {
46             throw new RuntimeException(e);
47         }
48     }
49 
50     @Override
51     public void updateAccount(Account acc) {
52         try {
53             runner.update("update account set name=? , money=? where id = ? " , acc.getName(),acc.getMoney(),acc.getId());
54         } catch (Exception e) {
55             throw new RuntimeException(e);
56         }
57     }
58 
59     @Override
60     public void deleteAccount(Integer accountId) {
61         try {
62             runner.update("delete from account where id = ? " , accountId );
63         } catch (Exception e) {
64             throw new RuntimeException(e);
65         }
66     }
67 }

 

service layer:

 1 package cn.mepu.service.imp;
 2 
 3 
 4 import cn.mepu.dao.AccountDao;
 5 import cn.mepu.domain.Account;
 6 import cn.mepu.service.AccountService;
 7 
 8 import java.util.List;
 9 
10 /**
11  * @author shkstart
12  * @create 2019-11-08 10:12
13  */
14 
15 public class AccountServiceImp implements AccountService {
16 
17     private AccountDao dao;
18 
19     public void setDao(AccountDao dao) {
20         this.dao = dao;
21     }
22 
23     @Override
24     public List<Account> findAllAccount() {
25         return dao.findAllAccount();
26     }
27 
28     @Override
29     public Account findAccountById(Integer accountId) {
30         return dao.findAccountById(accountId);
31     }
32 
33     @Override
34     public void saveAccount(Account acc) {
35         dao.saveAccount(acc);
36     }
37 
38     @Override
39     public void updateAccount(Account acc) {
40         dao.updateAccount(acc);
41     }
42 
43     @Override
44     public void deleteAccount(Integer accountId) {
45     dao.deleteAccount(accountId);
46     }
47 }

 

servlet layer:

 1 package cn.mepu.service;
 2 
 3 import cn.mepu.domain.Account;
 4 import org.junit.Test;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 import java.util.List;
 9 
10 /**
11  * @author shkstart
12  * @create 2019-11-08 10:45
13  */
14 public class AccountServiceTest {
15     @Test
16     public  void testFindAll () {
 . 17      // 1. Get the container 
18 is          the ApplicationContext AC = new new the ClassPathXmlApplicationContext ( "bean.xml" );
 . 19      // 2. give business layer objects 
20 is          the AccountService-Service = (the AccountService) ac.getBean ( "AccountService" );
 21      // 3. The method of performing 
22 is          List <the Account> Accounts = service.findAllAccount ();
 23 is          for (the Account Account: Accounts) {
 24              System.out.println ( "Account =" + Account);
 25          }
 26 is  
27      }
28  
29      @Test
 30      public  void testFindOne () {
 31 is          // 1. Get the container 
32          the ApplicationContext AC = new new the ClassPathXmlApplicationContext ( "bean.xml" );
 33 is          // 2. give business layer objects 
34 is          the AccountService-Service = (the AccountService) AC. the getBean ( "AccountService" );
 35          // 3. The method of performing 
36          the Account Account service.findAccountById = (. 1 );
 37 [          System.out.println (Account);
 38 is      }
 39  
40      @Test
 41 is      public  voidtestSave () {
 42 is          // 1. Get the container 
43 is          the ApplicationContext AC = new new the ClassPathXmlApplicationContext ( "bean.xml" );
 44 is          // 2. give business layer objects 
45          the AccountService-Service = (the AccountService) ac.getBean ( "AccountService" );
 46          @ 3 to perform a method 
47          service.saveAccount ( new new the Account (. 1, "DDD", 1234 ));
 48      }
 49  
50      @Test
 51 is      public  void testUpdate () {
 52 is          // 1. Get the container 
53 is          the ApplicationContext AC =new new the ClassPathXmlApplicationContext ( "bean.xml" );
 54 is          // 2. give business layer objects 
55          the AccountService-Service = (the AccountService) ac.getBean ( "AccountService" );
 56 is          // 3. performing the method 
57 is          service.updateAccount ( new new the Account ( . 1, "DDD", 2345 ));
 58      }
 59  
60      @Test
 61 is      public  void TestDelete () {
 62 is          // 1. Get the container 
63 is          the ApplicationContext AC = new new the ClassPathXmlApplicationContext ( "bean.xml" );
 64          // 2. give business layer objects
65          the AccountService-Service = (the AccountService) ac.getBean ( "AccountService" );
 66          // 3. The method of performing 
67          service.deleteAccount (. 4 );
 68      }
 69 }

 

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">
    <!- <! -->Scan inform spring loading the container is not constrained to be scanned beans package configuration desired, but is referred to as the constraint in the context namespace
    配置service-->
    <bean id="accountService" class="cn.mepu.service.imp.AccountServiceImp">
<!--        注入dao-->
        <property name="dao" ref="accountDao"></property>
    </bean>

<!--    配置dao-->
    <bean id="accountDao" class="cn.mepu.dao.imp.AccountDaoImp">
<!--        注入QueryRunner runner-->
        <property name="runner" ref= "Runner" > </ Property > 
    </ bean > 

<-!     Configuration injection QueryRunner scope thread-safe -> 
    < bean the above mentioned id = "Runner" class = "org.apache.commons.dbutils.QueryRunner" scope = " the prototype " > 
<-!         implantation data source -> 
        < constructor Arg- name =" DS " REF =" the dataSource " > </ constructor-Arg > 
    </ the bean > 

<-!     configuration data source -> 
    <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/javaee"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

</beans>

 

Guess you like

Origin www.cnblogs.com/aikang525/p/11819167.html