一 环境搭建
第一步:导入依赖
<dependencies>
<!--AOP相关的依赖-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--IOC相关的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- 数据库相关依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--与事务相关的-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
第二步:创建spring-config的配置文件
主要作用:创建连接池对象DruidDataSource,再通过连接池对象创建JdbcTemplate对象,相当于statement
<?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">
<!--数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybaties?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--JdbcTemplate-->
<bean name="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注册dataSource操作-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启注解扫描-->
<context:component-scan base-package="com.ls.service"/>
<context:component-scan base-package="com.ls.dao"/>
</beans>
第三步:创建dao接口及实现类
加上这两个注解,@Controller相当于在配置文件中创建了一个bean,
@Resources能通过类和名称自动注入,就把配置文件创建的JdbcTemplate注入了
第四步:创建Service接口及实现类
同样的方式将userDao注入里面
这样环境就搭建好了,可以进行CRUD了
二 相关的操作
返回结果封装为list集合
List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
返回结果封装为对象
User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class));
插入一条数据
int update = jdbcTemplate.update(sql, user.getUsername(), user.getBirthday(), user.getSex(), user.getAddress());
一次插入多条数据
一条数据就是一条数组,将数据添加到list集合
public int[] addUsers(List<Object[]> users) {
String sql = "insert into user(username,sex,birthday,address)values(?,?,?,?)";
int[] ints = jdbcTemplate.batchUpdate(sql, users);
return ints;
}
这里只记对我重点的,后面mybatis用不到这些