当前位置:网站首页>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 .
边栏推荐
- 【idea插件神器】教你如何使用IDEA一键set实体类中所有属性
- Implementation of simple upload function in PHP development
- Database date types are all 0
- The most detailed installation of windows10 virtual machine, install virtual machine by hand, and solve the problem that the Hyper-V option cannot be found in the home version window
- 11. < tag dynamic programming and subsequence, subarray> lt.115. Different subsequences + Lt. 583. Deletion of two strings DBC
- C language: some self realization of string functions
- Performance test classification
- MySQL practice 45 lectures
- 个人写的一个文件上传工具网站
- 解决Oracle使用in语句不能超过1000问题
猜你喜欢

mysql 为查询结果增加序号

Invalid bound statement (not found): com.exam.mapper.UserMapper.findbyid

7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces
![[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)

Microservice failure mode and building elastic system

Scope, execution process and life cycle of bean

Flask Development & get/post request

From the basic concept of micro services to core components - explain and analyze through an example

IDEA使用dev-tool实现热部署

Test Development - UI testing in automated testing
随机推荐
架构设计思考之一(SSO设计)
Confused, I'm going to start running in the direction of [test]
New modularity in ES6
YUV to uiimage
个人写的一个文件上传工具网站
Making RPM packages with nfpm
【SLAM】LVI-SAM解析——综述
【idea插件神器】教你如何使用IDEA一键set实体类中所有属性
FreeRTOS personal notes - task notification
Redis 之布隆过滤器
The research group passed the thesis defense successfully
The solution after the samesite by default cookies of Chrome browser 91 version are removed, and the solution that cross domain post requests in chrome cannot carry cookies
注册中心服务eureka 切换到 nocas遇到的问题
【计算机三级信息安全】信息安全保障概述
Scope, execution process and life cycle of bean
Thinking on multi system architecture design
First acquaintance with C language (1)
Struct模块到底有多实用?一个知识点立马学习
FreeRTOS个人笔记-任务通知
2022 summer practice (PowerDesigner tutorial learning record) (first week)