当前位置:网站首页>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>边栏推荐
猜你喜欢

Thinking on multi system architecture design

CentOS7安装MySQL5.7

Flask Development & get/post request

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

BigDecimal 进行四舍五入 四舍六入和保留两位小数

测试开发---自动化测试中的UI测试

11. < tag dynamic programming and subsequence, subarray> lt.115. Different subsequences + Lt. 583. Deletion of two strings DBC

Redis 之布隆过滤器

21 day SQL punch in summary

mysql 为查询结果增加序号
随机推荐
First acquaintance with C language (2)
openjudge:大小写字母互换
regular expression
C language: some self realization of string functions
pytorch使用hook获得特征图
List < long >, list < integer > convert each other
What are the methods of array objects in Es5 and what are the new methods in ES6
openjudge:矩阵乘法
低照度图像数据集
openjudge:校园食宿预订系统
latex使用\hl进行高亮时遇到引用总是报错,显示少了括号或者多了括号
MySQL date and time function, varchar and date are mutually converted
2021csdn blog star selection, mutual investment
New methods and features of ES6 built-in objects
注册中心服务eureka 切换到 nocas遇到的问题
蒙特卡罗方法求解圆周率π并用turtle画点,以及完成进度条问题
Tomato timing dimming table lamp touch chip-dlt8t10s-jericho
Personal summary of restful interface use
使用navicat或plsql导出csv格式,超过15位数字后面变成000(E+19)的问题
2022 summer practice (first week)