携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第3天,点击查看活动详情
今天来学习一下基于spring jdbc 增删改查
1.新建工程
2.在pom.xml中引入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--引入jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--引入mysql 这边的mysql和对应数据库版本一直就可以-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency>
3.在rescouce目录下创建applicationContext.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置数据源
url表示数据库连接语句,用于说明连接哪一个数据库,用哪种编码等信息
username表示数据库账户
password表示数据库密码-->
<bean id="dataScource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/imooc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
</beans>
4.基本的数据结构如下
CREATE TABLE `hotel` (
`orderno` int(11) NOT NULL,
`city` varchar(20) NOT NULL,
`price` float(6,2) NOT NULL,
`hotelname` varchar(64) NOT NULL,
`arrivedate` date DEFAULT NULL,
`leavedate` date DEFAULT NULL,
PRIMARY KEY (`orderno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
5.创建Hotel实体类
创建规范如下:
- 根据数据库字段类型来创建
如:orderno字段是整型的,我们设置成 public Integer orderno,其他字段雷同 - 创建对应的get和set方法
public Integer getOrderNo() {
return orderNo;
}
public void setOrderNo(Integer orderNo) {
this.orderNo = orderNo;
}
3.最后重写toString方便调试
6.回到applicationContext.xml配置对应的jdbcTemplate
<!-- jdbcTemplate 是我们进行数据操作的一个bean-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataScource"/>
</bean>
7.创建dao类
在dao类引入对应的jdbcTemplate和设置对应jdbcTemplate get和set方法
在application定义hotelDao的bean和注入对象jdbcTemplate
<!-- 定义dao的bean 注入的对象为 jdbcTemplate-->
<bean id="hotelDao" class="com.imooc.spring.dao.HotelDao">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
8.查找单条数据的方法
public Hotel findByOrderNo(Integer orderNo){
String sql = "select * from hotel where orderNo=?";
Hotel hotel = jdbcTemplate.queryForObject(sql,new Object[]{orderNo},new BeanPropertyRowMapper<Hotel>(Hotel.class));
return hotel;
}
9.根据多条数据方法
public List<Hotel> findByCity(String city){
String sql = "select * from hotel where city=?";
List hotel = jdbcTemplate.query(sql,new Object[]{city},new BeanPropertyRowMapper<Hotel>(Hotel.class));
return hotel;
}
10.新增数据方法
public void Insert(Hotel hotel){
String sql = "insert into hotel(orderNo,city,price,hotelname,arrivedate) values(?,?,?,?,?)";
int num = jdbcTemplate.update(sql,hotel.getOrderNo(),hotel.getCity(),hotel.getPrice(),hotel.hotelname,hotel.getArrivedate());
System.out.println(num);
}
11.删除数据方法
public void delete(Integer orderNo){
String sql = "delete from hotel where orderNo = ? ";
//返回影响行数
int num = jdbcTemplate.update(sql,new Object[]{orderNo});
System.out.println(num);
}
12.更新数据方法
public void update(Hotel hotel){
String sql = "update hotel set city=?,price=?,hotelname=? where orderno=?";
//返回影响行数
int num = jdbcTemplate.update(sql,new Object[]{hotel.getCity(),hotel.getPrice(),hotel.getHotelname(),hotel.getOrderNo()});
System.out.println(num);
}
13.创建方法入口main方法
import com.imooc.spring.dao.HotelDao;
import com.imooc.spring.entity.Hotel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Date;
public class Demo {
public static void main(String[] args) {
//引入对应applicationContext文件
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//根据bean名字获取对应的dao
HotelDao hotelDao = context.getBean("hotelDao", HotelDao.class);
//查询单条数据
System.out.println(hotelDao.findByOrderNo(10001));
//查询多条数据
System.out.println(hotelDao.findByCity("上海"));
//新增数据
Hotel hotel = new Hotel();
hotel.setOrderNo(100091);
hotel.setCity("北京");
hotel.setPrice(588);
hotel.setHotelname("酒店9");
hotel.setArrivedate(new Date());
hotelDao.Insert(hotel);
//删除数据
hotelDao.delete(10004);
//更新数
Hotel hotel3 = hotelDao.findByOrderNo(10003);
hotel.setCity("厦门123");
hotelDao.update(hotel);
}
}
返回结果为:
Hotel{orderNo=10001, city='北京', price=659.0, hotelname='酒店1', arrivedate=2011-05-08 00:00:00.0, leavedate=2011-05-11 00:00:00.0}
[Hotel{orderNo=10002, city='上海', price=799.0, hotelname='酒店2', arrivedate=2019-11-11 00:00:00.0, leavedate=2019-11-15 00:00:00.0}, Hotel{orderNo=10004, city='上海', price=699.0, hotelname='酒店4', arrivedate=2019-05-04 00:00:00.0, leavedate=2019-05-07 00:00:00.0}]
1
1
1
14.总结:
通过以上的步骤,我们学会了简单的操作步骤,实现了spring-jdbc的操作方式