关键词:Spring | 整合 | 持久层 | MyBatis
本专栏通过理论和实践相结合,系统学习框架核心思想及简单原理,原创不易,如果觉得文章对你有帮助,点赞收藏支持博主 ✨
这两行代码首先读取了配置文件,然后创建了 sqlSessionFactory
,配置文件的配置主要有三部分,第一部分用于配置别名,第二部分用于数据源配置,第三部分是Mapper文件的注册。
那既然是和 Spring
整合,我们就该想到 IOC 容器 和 DI 依赖注入,通过容器来创建这个sqlSessionFactory对象怎么样,通过依赖注入上面的配置如何
Spring配置文件配置
- 创建数据源
- 创建 SqlSessionFactory
- 注入数据源
- 指定实体类所在的包
- 指定配置文件的路径
- DAO接口的实现类 session
依赖导入
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.1.10.RELEASEversion>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.3.21version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.6version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.48version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
dependencies>
resources下创建Spring配置文件并配置
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/zhou?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
bean>
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="typeAliasesPackage" value="com.liu.entity"/>
<property name="mapperLocations">
<list>
<value>classpath:com.liu.mapper/*.xmlvalue>
list>
property>
bean>
<bean id="scanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
<property name="basePackage" value="com.liu.dao"/>
bean>
实体类 User
创建 entity
包,在实体包下创建实体类 User
package com.liu.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private Integer id;
private String name;
private String pwd1;
}
表 t_users
这里表还是用了上节的,部分SQL如下:
mysql> use zhou
Database changed
mysql> show tables;
+----------------+
| Tables_in_zhou |
+----------------+
| t_users |
+----------------+
1 row in set (0.00 sec)
mysql> desc t_users;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(12) | YES | | NULL | |
| pwd1 | varchar(12) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.03 sec)
DAO接口
创建 dao
包,并在此包下创建dao接口 UserDAO
package com.liu.dao;
import com.liu.entity.User;
public interface UserDAO {
void save(User user);
}
创建Mapper文件
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liu.dao.UserDAO">
<insert id="save" parameterType="User">
insert into t_users (name, pwd1)
values (#{name}, #{pwd1});
insert>
mapper>
测试
public class TestMybatisSpring {
@Test
public void t1(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("/application.xml");
// 规定获取的id首字母小写
UserDAO userDAO = (UserDAO) ctx.getBean("userDAO");
User user = new User();
user.setName("小张");
user.setPwd1("53434534");
userDAO.save(user);
}
}
查询数据库
mysql> select * from t_users;
+----+-----------+-----------+
| id | name | pwd1 |
+----+-----------+-----------+
| 1 | 周棋洛 | 123123917 |
| 2 | 小张 | 53434534 |
+----+-----------+-----------+
2 rows in set (0.02 sec)
座右铭:不要在乎别人如何看你,要在乎你自己如何看未来,看梦想,看世界…!
一起学习的可以私信博主或添加博主微信哦。
专栏:订阅专栏 ✅
关注:关注博主