当前位置:网站首页>Framework step by step easy-to-use process
Framework step by step easy-to-use process
2022-07-28 05:28:00 【MusicCodeM】
Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right
List of articles
1. dao layer
At first , I didn't learn orm( object relation mapping ) Frame time , Write the project using : JdbcTemplate The template operates the database .
- First use jdbc , establish connection object To get data , But trouble , Waste resources . So we used Database connection pool technology . Such as :Druid Druid
- establish Database connection pool object , Database password , The account number is written into the object , To get connenction To use .
- Due to the coupling problem , Will configure : Database password , Account number and other configurations , It's written in properties In file , Use classLoader Load injection into Database connection pool objects .
- Encapsulate a tool class , obtain connection object , Execute corresponding sql that will do .
- In the use of JdbcTemplate Gadget simplification jdbc The process of obtaining data , Just focus on writing sql, Just get the data .daoImpl Write the corresponding sql.
mybatis The process of :
The above problems : Use JdbcTemplate Gadget after , sql Statement is the same as Java Code Coupled ( It's written in java In the code ), And then the project will go online , Need to be right sql Optimize and modify , You need to be right about java Modify the source code of , Is not in line with the situation .
that , take sql Written in In profile . Solve the problem of high coupling .
It's on it : thought
- Import mybaits Of jar package , Need to use mybatis frame , Need to write a mybatis Core profile , One is mapper The mapping file ( Write about sql Statement file )
- mybatis The bottom layer also needs to be used Database connection pool technology .
- mybatis Core profile Write database Configuration of : Password etc. , Global profile : There are data sources, some running environment information , Location of mapping file, etc
Import dependence :
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
<scope>runtime</scope>
</dependency>;https://mybatis.org/mybatis-3/zh/index.html
establish MyBatis Configuration file for , And create test Class to create sqlSessionFactory
Use :
- load mybatis Global profile , To create a SqlSessionFactory object
- sqlSession The object is based on Specified in the global configuration file mapping Mapping file to query the corresponding sql To perform . according to id The query , Yes namespace Words , Write together .namespace.id
@Test
public void test() throws IOException {
String resource = "MyBatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
//2. obtain sqlSession example , It can directly execute the mapped sql sentence
/*sqlSession There are two parameters , The first is sql Unique identifier of , That is to say id The second is execution sql Parameters of */
SqlSession sqlSession = sqlSessionFactory.openSession();
Employee o = sqlSession.selectOne("com.Mybatis.EmployeeMapper.selectEMP", 1);
System.out.println(o);
sqlSession.close();
}
modify
Employee o = sqlSession.selectOne("com.Mybatis.EmployeeMapper.selectEMP", 1);
Employee o = sqlSession.selectOne("com.Mybatis.EmployeeMapper.selectEMP", "abc"); Parameter type cannot be determined
The version together obtains data in the above way , because Unable to determine the type of data , So interface programming is used
Mapping file corresponding to an interface . Mapping files namespace Write the full class name of the interface ,id Write the corresponding method name . that will do .
EmployeeMapperDap mapper = sqlSession.getMapper(EmployeeMapperDap.class); You can get the corresponding proxy object and directly execute sql.
How can this method find the corresponding sql Of ???
- It specifies sql Mapping location of , stay mybatis In the global configuration file .
- According to the interface class object , You can get its full class name , Compare namespace Value , The executed method can correspond to id, You can find the execution sql Statement .
边栏推荐
- SimpleDateFormat线程不安全和DateTimeFormatter线程安全
- Interpreting the source code of cfrunloopref
- ES6 new variable modifiers let and const, new basic data type symbol
- Scanf function of input and output function in C language
- VMware Workstation 与 Device/Credential Guard 不兼容。禁用 Device/Credential Guard
- How about ink cloud?
- After ruoyi generates the code corresponding to the database, what should I do to make the following image look like
- 导出excel,生成多个sheet页,并命名
- New arrow function in ES6
- 11.< tag-动态规划和子序列, 子数组>lt.115. 不同的子序列 + lt. 583. 两个字符串的删除操作 dbc
猜你喜欢
![[internal mental skill] - creation and destruction of function stack frame (C implementation)](/img/a9/81644ee9ffb74a5dc8ff1bc3977f49.png)
[internal mental skill] - creation and destruction of function stack frame (C implementation)

repackag failed: Unable to find main class

自定义Json返回数据

Database date types are all 0

Microservice failure mode and building elastic system

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

CentOS7安装MySQL5.7

Redis 之布隆过滤器

【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation

Thinking on multi system architecture design
随机推荐
【ARIXV2204】Neighborhood attention transformer
2021CSDN博客之星评选,互投
【SLAM】LVI-SAM解析——综述
BigDecimal 进行四舍五入 四舍六入和保留两位小数
微服务故障模式与构建弹性系统
【CVPR2022】Lite Vision Transformer with Enhanced Self-Attention
Tomato timing dimming table lamp touch chip-dlt8t10s-jericho
使用navicat或plsql导出csv格式,超过15位数字后面变成000(E+19)的问题
Antd setfieldsvalue warning problem cannot use 'setfieldsvalue' until you use 'getfielddecorator' or
Class class added in ES6
FreeRTOS个人笔记-任务通知
First acquaintance with C language (1)
IDEA使用dev-tool实现热部署
restFul接口使用个人总结
Message forwarding mechanism -- save your program from crashing
List<Long>,List<Integer>互相转换
regular expression
Specific differences between typedef and define
Thinking on multi system architecture design
类和对象【中】