当前位置:网站首页>Mabtis (I) basic use of framework
Mabtis (I) basic use of framework
2022-07-28 05:33:00 【It seems that I have known you for a long time】
1、 Introduce
MyBatis It's supporting Ordinary SQL Inquire about , stored procedure and Advanced mapping Excellent persistence layer framework for .MyBatis Eliminated almost all of them JDBC Manual setting of code and parameters and retrieval of result sets .MyBatis You can use simple XML Or annotations for configuring and raw mapping , The interface and Java Of POJO( Entity class object ) Maps to records in the database . Semi automated framework . Must write sql sentence .
2、 Basic use process
2.1 Import jar package
Use mabtis The framework needs to import two jar package , One is database driven jar package , Used to link databases . One is mabtis frame jar package .
maven Warehouse preparation jar Packet dependency
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>2.2 Add the configuration file of the framework conf.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- Configuration of data source :name The value of is fixed value The value of should be modified according to the customer -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydb?serverTimezone=Asia/Shanghai" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
</configuration>
2.3 Write entity class
package com.xrx.entity;
import org.omg.CORBA.StringHolder;
public class User {
private int id;
private String userName;
private String password;
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.4 Definition User Tabular sql The mapping file UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ykq.mybatis_test.test1.userMapper">
<select id="getUser" parameterType="int" resultType="User">
select * from users where id=#{id}
</select>
</mapper>2.5 stay conf.xml Register in the file UserMapper.xml
<mappers>
<mapper resource="userMapper.xml"/>
</mappers>2.6 Write a test class to execute SQL sentence
public class Test {
public static void main(String[] args) throws IOException {
String resource = "conf.xml";
// load mybatis Configuration file for ( It also loads the associated mapping file )
Reader reader = Resources.getResourceAsReader(resource);
// structure sqlSession Our factory
SqlSessionFactory sessionFactory = new
SqlSessionFactoryBuilder().build(reader);
// Create executable mapping file sql Of sqlSession
SqlSession session = sessionFactory.openSession();
// mapping sql The identity string of
String statement = "com.atguigu.mybatis.bean.userMapper"+".selectUser";
// Executing the query returns a unique user Object's sql
User user = session.selectOne(statement, 1);
System.out.println(user);
}
}increase 、 Delete 、 Change , These operations are all in UserMapper.xml Written in Chinese sql, Then test execution sql That's it
// increase sql
<insert id="insertUser" parameterType="User">
insert into users(name, age) values(#{name}, #{age});
</insert>// Delete sql
<delete id="deleteUser" parameterType="int">
delete from users where id=#{id}
</delete>// modify sql
<update id="updateUser" parameterType="com.ykq.ibatis.bean.User">
update users set name=#{name},age=#{age} where id=#{id}
</update>边栏推荐
- 接口幂等性问题
- 解决Oracle使用in语句不能超过1000问题
- Thinking on multi system architecture design
- 【idea插件神器】教你如何使用IDEA一键set实体类中所有属性
- Bean的作用域、执行流程、生命周期
- Mysql数据库索引(innodb引擎)
- 7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces
- pytorch使用hook获得特征图
- 多线程进阶:synchronized底层原理,锁优化、锁升级的过程
- JVM篇 笔记3:类加载与字节码技术
猜你喜欢

PC端-bug记录

科研论文写作方法:在方法部分添加分析和讨论说明自己的贡献和不同
![[computer level 3 information security] overview of information security assurance](/img/f0/a72e61fda58ea93ca4e9db7274f6e3.png)
[computer level 3 information security] overview of information security assurance

Operation and use of collection framework

Long和Integer如何进行比较,为什么报错

【SLAM】LVI-SAM解析——综述

After ruoyi generates the code corresponding to the database, what should I do to make the following image look like

多线程进阶:synchronized底层原理,锁优化、锁升级的过程

MySQL date and time function, varchar and date are mutually converted

GET与POST区别
随机推荐
repackag failed: Unable to find main class
IO流的使用
图像增强——MSRCR
[computer level 3 information security] overview of information security assurance
MySQL practice 45 lectures
JUC笔记
Operation and use of collection framework
Personal summary of restful interface use
VMware Workstation 与 Device/Credential Guard 不兼容。禁用 Device/Credential Guard
JVM篇 笔记4:内存模型
Response < t > class
New methods and features of ES6 built-in objects
visio如何快速生成相同的图案,生成图像矩阵
List < long >, list < integer > convert each other
List<Long>,List<Integer>互相转换
2021CSDN博客之星评选,互投
JMeter related knowledge sorting
Response<T>类
[singleton mode] thread safety of lazy mode
(黑马)MYSQL初级-高级笔记(博主懒狗)