jdbc spring configuration learning learning

The blog links to learning additions and deletions to change search the database by spring.

Project structure:

Install mysql database: a lot of people blog or garden csdn have compressed exe version and install version is not in the narrative.

Visual Database software installation and crack navicat: https://www.jianshu.com/p/42a33b0dda9c

Account.java

 1 package com.itheima.jdbc;
 2 
 3 public class Account {
 4     private Integer id;
 5     private String username;
 6     private Double balance;
 7     @Override
 8     public String toString() {
 9         return "Account [id=" + id + ", username=" + username + ", balance=" + balance + "]";
10     }
11     
12     public Integer getId() {
13         return id;
14     }
15     public void setId(Integer id) {
16         this.id = id;
17     }
18     public String getUsername() {
19         return username;
20     }
21     public void setUsername(String username) {
22         this.username = username;
23     }
24     public Double getBalancce() {
25         return balance;
26     }
27     public void setBalancce(Double balancce) {
28         this.balance = balancce;
29     }
30     
31     
32     
33 }

AccountDao.java

 1 package com.itheima.jdbc;
 2 
 3 import java.util.List;
 4 
 5 public interface AccountDao {
 6     public int addAccount(Account account);
 7     public int updateAcount(Account account);
 8     public int deleteAccount(int id);
 9     public Account findAccountById(int id);
10     public List<Account> listAllAccounts();
11 }

AccountDaoImpl.java

 1 package com.itheima.jdbc;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.jdbc.core.BeanPropertyRowMapper;
 6 import org.springframework.jdbc.core.JdbcTemplate;
 7 import org.springframework.jdbc.core.RowMapper;
 8 
 9 public class AccountDaoImpl implements AccountDao {
10     
11     //声明JdbcTemplate属性及setter方法
12     private JdbcTemplate jdbcTemplate;
13     
14     
15     public JdbcTemplate getJdbcTemplate() {
16         return jdbcTemplate;
17     }
18 
19     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
20         this.jdbcTemplate = jdbcTemplate;
21     }
22 
23     @Override
24     public int addAccount(Account account) {
25         // TODO Auto-generated method stub
26         
27         String sqlString="insert into account (username,balance)value(?,?)";
28         Object[] objects=new Object[] {
29                 account.getUsername(),
30                 account.getBalancce()
31         };
32         int num=this.jdbcTemplate.update(sqlString, objects);
33         return num;
34     }
35 
36     @Override
37     public int updateAcount(Account account) {
38         // TODO Auto-generated method stub
39         String sqlString="update account set username=?,balance=?where id=?";
40         Object[] objects=new Object[] {
41                 account.getUsername(),
42                 account.getBalancce(),
43                 account.getId()
44         };
45         int num=this.jdbcTemplate.update(sqlString, objects);
46         return num;
47     }
48 
49     @Override
50     public int deleteAccount(int id) {
51         // TODO Auto-generated method stub
52         String sqlString="delete from account where id=?";
53         int num=this.jdbcTemplate.update (sqlString, ID);
 54 is          return NUM;
 55      }
 56 is  
57 is      @Override
 58      public the Account findAccountById ( int ID) {
 59          // the TODO Auto-Generated Method Stub 
60          String sqString = "SELECT * WHERE ID = Account from ? " ;
 61  //         Create a BeanPropertyRowMapper 
62 is          RowMapper <the Account> = rowMapper new new BeanPropertyRowMapper <the Account> (the Account. class );
 63 is  //         the id bound to sql statement by RowMapper returns a record object returns a single row 
64          
65         return this.jdbcTemplate.queryForObject(sqString, rowMapper,id);
66     }
67 
68     @Override
69     public List<Account> listAllAccounts() {
70         // TODO Auto-generated method stub
71         String sqString="select * from account ";
72         RowMapper<Account>rowMapper=new BeanPropertyRowMapper<Account>(Account.class);
73         
74         return this.jdbcTemplate.query(sqString, rowMapper);
75     }
76 
77 }

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
 6     <!-- 配置数据源 -->
 7     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
Database-driven<-!8         -> 
. 9          < Property name = "driverClassName" value = "com.mysql.jdbc.Driver" > </ Property > 
10          <-! Link to the database URL -> 
. 11          < Property name = "URL" value = " jdbc: MySQL: // localhost: 3306 / = to true useUnicode the Spring & amp; characterEncoding = UTF-8 "? > </ Property > 
12          <-! link to the database user name -> 
13          < Property name =" username " value = "root" > </ Property > 
14          - <! Password link database     -->
15         <property name="password" value="123456"></property>
16     </bean>
17     <!-- 配置jdbc模板 -->
18     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
19         <property name="dataSource" ref="dataSource"></property>
20     </bean>
21     
22     <bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl">
23         <!-- 将JdbcTemplate注入到accountDao中 -->
24         <property name="jdbcTemplate" ref="jdbcTemplate"></property>
25     </bean>
26 </beans>

JdbcTemplateTest.java

 1 package com.itheima.jdbc;
 2 
 3 import java.util.List;
 4 
 5 import javax.sound.midi.VoiceStatus;
 6 
 7 import org.junit.Test;
 8 import org.springframework.context.ApplicationContext;
 9 import org.springframework.context.support.ClassPathXmlApplicationContext;
10 import org.springframework.jdbc.core.JdbcTemplate;
11 
12 public class JdbcTemplateTest {
13 
14 
15     
16       @Test 
17       public void mainTest() {
18           ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); 
19           JdbcTemplate jdbcTemplate=(JdbcTemplate) applicationContext.getBean("jdbcTemplate");
20           jdbcTemplate.execute("create table account("+
21                                   "id int primary key auto_increment,"+ 
22                                   "username varchar(50),"+
23                                   "balance double)"); 
24           System.out.println("数据库表创建成功");
25           }
26      
27     @Test
28     public void addAccountTest() {
29         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
30         AccountDao accountDao=(AccountDao) applicationContext.getBean("accountDao");
31         Account account=new Account();
32         account.setUsername("tom");
33         account.setBalancce(1000.00);
34         int num=accountDao.addAccount(account);
35         if(num>0) {
36             System.out.println("插入了"+num+"条数据");
37         }else {
38             System.out.println("fail");
39         }
40     }
41     
42     @Test
43     public void deleteAccountTest() {
44         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
45         AccountDao accountDao=(AccountDao) applicationContext.getBean("accountDao");
46         int num=accountDao.deleteAccount(1);
47         if (num>0) {
48             System.out.println("删除成功");
49         }else {
50             System.out.println("删除失败");
51         }
52     }
53     
54     @Test
55     public void findAccountByIdTest() {
56         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
57         AccountDao accountDao=(AccountDao)applicationContext.getBean("accountDao");
58         Account account=accountDao.findAccountById(2);
59         System.out.println(account);
60     }
61     @Test
62     public void listAllAccount() {
63         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
64         AccountDao accountDao=(AccountDao)applicationContext.getBean("accountDao");
65         List<Account> accounts=accountDao.listAllAccounts();
66         for(Account account:accounts) {
67             System.out.println(account);
68         }
69     }
70 
71 }

Experiment, so I have to write the url: jdbc: mysql: // localhost: 3306 / spring useUnicode = true & amp; characterEncoding = utf-8 otherwise it will error?.

Guess you like

Origin www.cnblogs.com/2312947032zyk/p/11222354.html