For more information, see Mybatis series (1) --- Mybatis getting started, mybatis --- mybatis
In the last two years, springmvc + mybatis is still very popular in this kind of matching. I have never really touched on mybatis, and I am so busy recently. I just want to learn about mybatis. Based on your learning progress, I plan to conduct a series of tutorials on mybatis, record your learning history, and explore mybatis for friends who have not been in touch with it. This series of tutorials is intended to be in-depth (analyzed by mybatis source code), so it may take several days to complete the update. Now, let's start this mybatis learning journey. This is the first tutorial. Let's write a demo to get to know mybatis.
For convenience, I used maven. I will not introduce how to use maven. Maven has never been used and does not affect reading.
1. Mybatis environment setup and simple instances
1. create a web project and add a dependency package: mybatis package, database driver package (mysql is used), and log package (log4j is used). Because my maven project is used, then it is easy to add the dependency package, directly in the pom. add dependency in xml.
Pom. xml:
<Dependencies> <! -- Add junit --> <dependency> <groupId> junit </groupId> <artifactId> junit </artifactId> <version> 4.11 </version> <scope> test </scope> </dependency> <! -- Add log4j --> <dependency> <groupId> log4j </groupId> <artifactId> log4j </artifactId> <version> 1.2.16 </version> </dependency> <! -- Add mybatis --> <dependency> <groupId> org. mybatis </groupId> <artifactId> mybatis </artifactId> <version> 3.2.6 </version> </dependency> <! -- Add a mysql driver --> <dependency> <groupId> mysql </groupId> <artifactId> mysql-connector-java </artifactId> <version> 5.1.12 </version> </dependency> </dependencies>
2. Configure log4j and mybatis
Create a configuration file log4j. properties for configuring log4j in classpath, and create a configuration file configuration. xml for configuring Mybatis (the file can be named casually ). I will not talk about the configuration of log4j. Here we mainly talk about configuration. xml:
Configuration. xml:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE configuration 3 PUBLIC "-// mybatis.org//DTD Config 3.0 // EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <! -- Specify the properties configuration file. Here we configure database-related items --> 8 <properties resource = "dbConfig. properties"> </properties> 9 10 <! -- Specify Mybatis to use log4j --> 11 <settings> 12 <setting name = "logImpl" value = "LOG4J"/> 13 </settings> 14 15 <environments default = "development"> 16 <environment id = "development"> 17 <transactionManager type = "JDBC"/> 18 <dataSource type = "POOLED"> 19 <! -- 20 if the properties file for database configuration is not specified above, you can directly configure 21 <property name = "driver" value = "com. mysql. jdbc. driver "/> 22 <property name =" url "value =" jdbc: mysql: // localhost: 3306/test1 "/> 23 <property name =" username "value =" root "/> 24 <property name =" password "value =" root "/> 25 --> 26 27 <! -- The database configuration file is specified above, the configuration file also corresponds to the four attributes --> 28 <property name = "driver" value = "$ {driver}"/> 29 <property name = "url" value =" $ {url} "/> 30 <property name =" username "value =" $ {username} "/> 31 <property name =" password "value =" $ {password} "/> 32 33 </dataSource> 34 </environment> 35 </environments> 36 37 <! -- Ing file, the essence of mybatis, later I will elaborate --> 38 <mappers> 39 <mapper resource = "com/dy/dao/userDao-mapping.xml"/> 40 </mappers> 41 42 </configuration>View Code
3. Start to write Demo
First, create a table user in mysql database test1:
Then, write the java code.
View my project structure:
First, compile an object class User: User class to correspond to the User table.
User:
1 package com. dy. entity; 2 3 public class User {4 5 private int id; 6 private String name; 7 private String password; 8 private int age; 9 private int deleteFlag; 10 11 public int getId () {12 return id; 13} 14 public void setId (int id) {15 this. id = id; 16} 17 public String getName () {18 return name; 19} 20 public void setName (String name) {21 this. name = name; 22} 23 public String getPassword () {24 return password; 25} 26 public void setPassword (String password) {27 this. password = password; 28} 29 public int getAge () {30 return age; 31} 32 public void setAge (int age) {33 this. age = age; 34} 35 public int getDeleteFlag () {36 return deleteFlag; 37} 38 public void setDeleteFlag (int deleteFlag) {39 this. deleteFlag = deleteFlag; 40} 41 42}View Code
Write another UserDao interface:
UserDao:
1 package com. dy. dao; 2 3 import java. util. list; 4 5 import com. dy. entity. user; 6 7 public interface UserDao {8 9 public void insert (User user); 10 11 public User findUserById (int userId); 12 13 public List <User> findAllUsers (); 14 15}View Code
Write another userDao-mapping.xml (can be named casually ):
UserDao-mapping.xml:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE mapper 3 PUBLIC "-// ibatis.apache.org//DTD Mapper 3.0 // EN" 4 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> 5 <mapper namespace = "com. dy. dao. userDao "> 6 7 <select id =" findUserById "resultType =" com. dy. entity. user "> 8 select * from user where id =#{ id} 9 </select> 10 11 </mapper>View Code
The userDao-mapping.xml is equivalent to the implementation of UserDao, and the User entity class is also successfully associated with the data table User.
4. Write the junit test code UserDaoTest below:
UserDaoTest:
1 public class UserDaoTest {2 3 @ Test 4 public void findUserById () {5 SqlSession sqlSession = getSessionFactory (). openSession (); 6 UserDao userMapper = sqlSession. getMapper (UserDao. class); 7 User user = userMapper. findUserById (2); 8 Assert. assertNotNull ("no data found", user); 9} 10 11 // Mybatis obtains SqlSession through SqlSessionFactory, and then interacts with the database through SqlSession 12 private static SqlSessionFactory getSessionFactory () {13 SqlSessionFactory sessionFactory = null; 14 String resource = "configuration. xml "; 15 try {16 sessionFactory = new SqlSessionFactoryBuilder (). build (Resources 17. getResourceAsReader (resource); 18} catch (IOException e) {19 e. printStackTrace (); 20} 21 return sessionFactory; 22} 23 24}View Code
Okay, so that a simple mybatis demo can run successfully. Through this demo, you can also see the running mechanism of mybatis. If you do not know it, it doesn't matter. I started to explain mybatis in the next article.
Query a record using mybatis, and return a map. The value of map is empty. This one-key value pair will be filtered out by mybatis.
Check the version of Mybatis. 3.0.4. This is your display method.
More than 3.1 has a parameter to control whether to filter NULL values.
A small case of mybatis development. I hope the content will be detailed and I'm so anxious.
When there are too many other users .... Why did you send a demo to you without leaving an email address ....
Base with mybatis
There are also examples of integration with strus and spring. You can give me some points. You can't even snap them out.