Environment configuration of MyBatis
I wanted to learn about mybatis a long time ago. It is said that many companies use this framework. In the past, I used DBCP, Which is outdated. Hibernate felt too big. Now I want to learn this neutral framework.
Practical application of Java: Mybatis implements addition, deletion, and modification of a single table
[Java] [Mybatis] physical paging implementation
Mybatis quick start tutorial
Test batis's batch data operations
Batch insert operation for List <Object> Object List in Mybatis
The first step is environment configuration. I use maven to create a project. The pom. xml file is as follows:
<Project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<ModelVersion> 4.0.0 </modelVersion>
<GroupId> com. bird </groupId>
<ArtifactId> concursey </artifactId>
<Version> 0.0.1-SNAPSHOT </version>
<Packaging> jar </packaging>
<Name> concursey </name>
<Url> http://maven.apache.org </url>
<Properties>
<Project. build. sourceEncoding> UTF-8 </project. build. sourceEncoding>
</Properties>
<Dependencies>
<Dependency>
<GroupId> junit </groupId>
<ArtifactId> junit </artifactId>
<Version> 3.8.1 </version>
<Scope> test </scope>
</Dependency>
<Dependency>
<GroupId> org. mybatis </groupId>
<ArtifactId> mybatis </artifactId>
<Version> 3.2.7 </version>
</Dependency>
<Dependency>
<GroupId> mysql </groupId>
<ArtifactId> mysql-connector-java </artifactId>
<Version> 5.1.32 </version>
</Dependency>
</Dependencies>
</Project>
We have to create a database and a corresponding table for demonstration. We will not write it here. We will directly use the corresponding JavaBean
Package com. bird. mybatis. bean;
Public class Users {
Private int id;
Private String name;
Private int age;
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getAge (){
Return age;
}
Public void setAge (int age ){
This. age = age;
}
@ Override
Public String toString (){
Return "Users [id =" + id + ", name =" + name + ", age =" + age + "]";
}
}
Create a mapper. xml file for the corresponding JavaBean. The content is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE mapper
PUBLIC "-// mybatis.org//DTD Mapper 3.0 // EN"
Http://mybatis.org/dtd/mybatis-3-mapper.dtd>
<Mapper namespace = "com. bird. mybatis. bean. userMapper">
<! -- Obtain the corresponding value based on the ID -->
<Select id = "getUser" parameterType = "int" resultType = "com. bird. mybatis. bean. Users">
Select * from users where id =#{ id}
</Select>
</Mapper>
Of course, there is a global file for mybatis. The conf. xml content is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE configuration
PUBLIC "-// mybatis.org//DTD Config 3.0 // EN"
Http://mybatis.org/dtd/mybatis-3-config.dtd>
<Configuration>
<! -- Development: development Mode work: work mode -->
<Environments default = "development">
<Environment id = "development">
<TransactionManager type = "JDBC"/>
<DataSource type = "POOLED">
<Property name = "driver" value = "com. mysql. jdbc. Driver"/>
<Property name = "url" value = "jdbc: mysql: // localhost: 3306/mybatis"/>
<Property name = "username" value = "root"/>
<Property name = "password" value = "root"/>
</DataSource>
</Environment>
</Environments>
<Mappers>
<Mapper resource = "com/bird/mybatis/bean/userMapper. xml"/>
</Mappers>
</Configuration>
Finally, the test code
Package com. bird. mybatis. bean;
Import java. io. Reader;
Import org. apache. ibatis. io. Resources;
Import org. apache. ibatis. session. SqlSession;
Import org. apache. ibatis. session. SqlSessionFactory;
Import org. apache. ibatis. session. SqlSessionFactoryBuilder;
Public class Test {
Public static void main (String [] args) throws Exception {
String resource = "conf. xml ";
Reader reader = Resources. getResourceAsReader (resource );
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder (). build (reader );
SqlSession session = sessionFactory. openSession ();
String statement = "com. bird. mybatis. bean. userMapper. getUser ";
Users user = session. selectOne (statement, 1 );
System. out. println (user );
}
}
In general, it is very simple.
For more information about MyBatis, click here.
MyBatis: click here
This article permanently updates the link address: