针对数据库操作,Spring框架提供了JdbcTemplate类。

1.Spring JDBC的配置

创建配置文件applicationContext.xml,添加如下代码:

 1    <!--配置数据源-->
 2     <bean 
 3           class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 4         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
 5         <property name="url" value="jdbc:mysql://localhost:3306/springjdbc"/>
 6         <property name="username" value="root"/>
 7         <property name="password" value="root"/>
 8     </bean>
 9     <!--配置JDBC模板-->
10     <bean >
11         <property name="dataSource" ref="dataSource"/>
12     </bean>
13     <!--配置注入类-->
14     <bean >
15         <property name="jdbcTemplate" ref="jdbcTemplate"/>
16     </bean>

2.创建Javabean和Dao接口

 1 public class User {
 2     private Integer id;
 3     private String name;
 4     private Integer age;
 5 
 6     public Integer getId() {  return id; }
 7 
 8     public void setId(Integer id) {
 9         this.id = id;
10     }
11 
12     public String getName() {
13         return name;
14     }
15 
16     public void setName(String password) {
17         this.name = password;
18     }
19 
20     public Integer getAge() { return age; }
21 
22     public void setAge(Integer age) {
23         this.age = age;
24     }
25 }
javabean

相关文章:

  • 2021-04-25
  • 2021-06-27
  • 2022-02-03
  • 2021-07-14
  • 2021-11-27
  • 2022-01-12
猜你喜欢
  • 2021-12-22
  • 2021-08-24
  • 2021-07-14
  • 2022-01-12
相关资源
相似解决方案