1.导入依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.8</version> </dependency>
2.创建spring配置文件 配置数据源
<!-- 1.配置数据源 --> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> <!-- 1.1.数据库驱动 --> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <!-- 1.2.连接数据库的url --> <property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&serverTimezone=UTC"/> <!-- 1.3.连接数据库的用户名 --> <property name="username" value="root"></property> <!-- 1.4.连接数据库的密码 --> <property name="password" value="123456"></property> </bean> <!-- 2配置JDBC模板 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!-- 默认必须使用数据源 --> <property name="dataSource" ref="dataSource"></property> </bean>
3.实体类
package com.springjdbc; public class Person { private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } private int age; private String name; }
4.Dao层
package com.springjdbc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; //用注解创建的对象就是类本身的类型,名字就是这个类的名字,首字母小写personDao @Repository public class PersonDao { // 1 实现依赖注入 // 2 生成成员变量get和set方法 // 3 实现自动注入 byType->byName (@Resource功能类似,只是该注解优先采用根据名称自动装配) @Autowired JdbcTemplate jdbcTemplate; public List<Person> findAllUser(){ RowMapper<Person> rowMapper = new BeanPropertyRowMapper<>(Person.class); return jdbcTemplate.query("select * from person",rowMapper); } public void addUser(String name,int age){ jdbcTemplate.update("insert into person values (null,?,?)",age,name); }//?为占位符 public void updateUser(String name,int age,int id){ jdbcTemplate.update("update person set name = ?,age = ? where id = ?",name,age,id); } public void deleteUser(int id){ jdbcTemplate.update("delete from person where id = ?",id); } }
5.测试
package com.springjdbc; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; /** * Hello world! * */ public class App { public static void main( String[] args ) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//加载spring容器,创建对象 PersonDao personDao = (PersonDao) applicationContext.getBean("personDao"); personDao.addUser("小花",18); List<Person> personList = personDao.findAllUser(); for(Person person : personList){ System.out.println(person.getName()+" "+person.getAge()); } System.out.println( "Hello World!" ); } }
标签:JDBC,spring,springframework,id,org,import,public From: https://www.cnblogs.com/dzs894330350/p/16868063.html