Create Java Project Project, build database build table insert data, execute into database



Add the core JAR for Mybatis

Add the mysql database connection driver

Add the core JARS required by the Log4J diary

Add the log4j.properties diary configuration file in the config directory

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
Copy the code

Note: There is one point that needs clarification. We need to set the level of the diary to DEBUG during development. This allows you to view more detailed and useful information. Change the level of the diary to INFO when the project is published.



Create entity classes for get,set, toString, no argument modification, parameter construction

public class User {

	private int id;
	private String lastName;
	private int sex;
Copy the code

Create the usermapper.xml configuration file

Then create a usermapper.xml configuration file in the package where the Pojo object User resides. This configuration file is used to configure the User object corresponding to the table add, delete, change, query statement. Generally, the configuration file is named mapper.xml

If the class is User, the corresponding configuration file is named usermapper. XML. If the class is Teacher, the corresponding configuration file is teachermapper. XML.

<? The 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" > <! The namespace attribute stands for namespace. Function is equivalent to defining a package name for the configuration file. In general. You can write two values, one is the full class name of the corresponding class and one is the case. The full class name of the processing interface for the corresponding class (described below). --> <mapper namespace="com.pojo.User"> <! ParameterType Specifies the type of the parameter. StatementId specifies the id of the select statement. ParameterType specifies the type of the parameter. <select ID ="selectUserById" parameterType="int" resultType="com.pojo.User"> select id, last_name lastName, sex from t_user where id = #{value} </select> </mapper>Copy the code

Create the mybatis-config.xml core configuration file in the config directory

<? The 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> <! --> <environments default="development"> <! --> <environment id="development"> <! -- transactionManager specifies what type of database transaction management to use. Type ="JDBC" indicates that transactions are enabled, and type="MANAGED" indicates that transactions are not directly controlled. Hand it over to the container -- rarely used. --> <transactionManager type="JDBC" /> <! Type ="UNPOOLED" --> <dataSource type="POOLED"> <dataSource type="POOLED"> <! <property name="driver" value=" com.mysql.jdbc.driver "/> <! - database access address - > < property name = "url" value = "JDBC: mysql: / / localhost: 3306 / mybatis" / > <! <property name="username" value="root" /> <! -- database password --> <property name="password" value="root" /> </dataSource> <mapper resource="com/pojo/ usermapper. XML "/> </mappers> </configuration>Copy the code

The test class:

Public class UserTest {@test public void test1() throws IOException {// Read the mybatis core configuration file InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory SqlSessionFactory = new SqlSessionFactoryBuilder().build(is); / / create a sqlSession object sqlSession sqlSession. = sqlSessionFactory openSession (); try { User user = sqlSession.selectOne("com.pojo.User.selectUserById", 1); System.out.println(user); } finally { sqlSession.close(); }}}Copy the code

How does it work? Example: the User User = sqlSession. SelectOne (” com. Pojo. User. SelectUserById “, 1);

First read the core configuration file of Mybatis

InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
Copy the code

The mybatis – config. The XML

<mappers> <! <mapper resource="com/pojo/ usermapper. XML "/> </mappers>Copy the code

Find the corresponding usermapper.xml, which is in it

<mapper namespace="com.pojo.User">
Copy the code

Find the corresponding User class

<select id="selectUserById" parameterType="int" resultType="com.pojo.User">
Copy the code

Find the corresponding selectUserById using the id id

<select id="selectUserById" parameterType="int" resultType="com.pojo.User">
	select id, last_name lastName, sex from t_user where id = #{value}
</select>
Copy the code

Mybatis configuration file configuration automatically prompt mybatis – config. XML file copy http://mybatis.org/dtd/mybatis-3-config.dtd then according to the operation as shown in the figure below, Find your mybatis – 3 – config. XxxMapper DTD file. The XML file winning http://mybatis.org/dtd/mybatis-3-mapper.dtd copy and then according to the operation as shown in the figure below, Find your mybatis-3-mapper. DTD file