当前位置:网站首页>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 .
边栏推荐
- Interpretation of afnetworking4.0 request principle
- 21 day SQL punch in summary
- block yandex bot
- regular expression
- I've been in an outsourcing company for two years, and I feel like I'm going to die
- You must configure either the server or JDBC driver (via the ‘serverTimezone)
- Message forwarding mechanism -- save your program from crashing
- HashSet add
- JMeter related knowledge sorting
- 多系统架构设计思考
猜你喜欢

You must configure either the server or JDBC driver (via the ‘serverTimezone)

在ruoyi生成的对应数据库的代码 之后我该怎么做才能做出下边图片的样子

类和对象【中】

SSLError

Mysql基本查询

【内功心法】——函数栈帧的创建和销毁(C实现)

Digital twin solutions inject new momentum into the construction of chemical parks

多线程进阶:volatile的作用以及实现原理

How practical is the struct module? Learn a knowledge point immediately

7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces
随机推荐
Interpretation of afnetworking4.0 request principle
Test Development - UI testing in automated testing
关于swagger中的localDateTime
Performance test classification
HashSet add
个人写的一个文件上传工具网站
多御安全浏览器将改进安全模式,让用户浏览更安全
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
Microservice failure mode and building elastic system
【单例模式】懒汉模式的线程安全问题
How to successfully test php7.1 connecting to sqlserver2008r2
regular expression
Oracle用sql查询某张表的字段信息(字段类型、长度等)
YUV to uiimage
restFul接口使用个人总结
ES6 new variable modifiers let and const, new basic data type symbol
DELL远程控制卡 使用ipmitools设置ipmi
php7.1 连接sqlserver2008r2 如何测试成功
CentOS7安装MySQL5.7
block yandex bot