Database operations using mybatis in java
Currently in the java project whether it is a web project or long connection tcp/udp/websocket, mytatis use has become more and more widespread, many development attack lions are clear ssh framework, where the h is hibernate, but with the progress of time, the use of more is ibatis, today to talk about is mytatis. In general, mytatis is actually an evolution of ibatis, so it was natural to compare ibatis and mytatis here.
1 The conditions for mapping are already different.
The root element in ibatis is sqlMapConfig and in mybatis is configuration.
The sqlMap element is used in ibatis and the mappers element is used in mybatis.
2 with spring framework, the configuration is also different.
3 mybatis supports bulk operations, xml configuration foreach can be.
4 The difference between $ and # in mybatis
I think we originally all used the # sign when passing a single value, but the $ symbol is also available for passing a value, but the $ conform is dynamically passed in for a particular value, or a result set.
And the $ symbol can cause sql injection problems, think of a scenario where # is only able to pass one value in, but $ can pass a whole bunch of stuff in today, then we will have no control over the value passed in $, so it will send sql injection problems.
Usage:
1 eclipse install mytatis
Download myBatis to generate pojo plugins, get features and plugins, assign both to eclipse software corresponding point features and plugins
2 Reopen eclipse to create three configuration files
(1) configuration.xml for configuring database access connection configuration and table operations xml configuration mappers
(2) dataBase.properties is used to configure the parameters of the database module
(3) Model.xml in Model represents the various operations of a table
All three are placed in the src/game/conf directory, and for reasons of space, not much is said about them, just communicate privately.
(5) Write sql statements in Model.xml, a simple example is as follows
(6) Declare the methods for this query in IUserOperation.java
public List selectModelByID(String user_id); Note that the function name is consistent with the configuration in the sql statement
(7) Execute static for loading mybatis database configuration
static {
try {
reader = Resources.getResourceAsReader("com/game/conf/Configuration.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
(8) Just implement the method in Dao
public Model selectModelByID(String user_id){
SqlSession session = sqlSessionFactory.openSession();
try {
IModelOperation userOperation = session
.getMapper(IModelOperation.class);
List gameData = userOperation.selectModelByID(user_id);
System.out.println("count:"+gameData.size());
for(Model data : gameData) {
System.out.println("uid:"+data.getUser_id()+";success_count"+data.getGame_success_count());
}
} finally {
session.close();
}
return null;
}
(9) Finally, just call the method in Dao in the logic.