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.


Recommended>>
1、Dont ever mess with a bull After 22 theft little brother comes up with this whole ethereum wallet security rundown hackers have to cry after reading it
2、Announcement on the arrangement plan of the provincial special fund for promoting economic development in 2018 for the development of information technology and information industry Internet application project bank
3、Fish and shrimp crab cheats fish and shrimp crab seethrough hangouts cheat auxiliary
4、Save graphics and links quickly Taggy lets you do it all seamlessly
5、Linxee Condenser Conference AllinOne debuts

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号