First, create a new JavaWeb project and import the jar package that mybatis depends on. At the same time, Mybatis is an operation on the database, so we need to create a new table user in the database for demonstration.
After creating the new table, we also need to create the corresponding entity class User.java and add set and get methods:
public class User {private String username;private String password;private int age;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}In Mybatis, we need to create a mapping file corresponding to the entity class userMapper.xml:
<?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"><!-- Specify a unique namespace for this mapper. The value of the namespace is conventionally set to the package name + sql map file name)--><mapper namespace="com.mybatis.mapping.userMapper"><!-- Write a query in the select tag. The value of the id attribute must be the only parameter type used to specify the query when using the parameterType attribute. The resultType attribute specifies the type of result set returned by the query--><!-- Get a user object based on the username query--><select id="getUser" parameterType="java.lang.String" resultType="com.mybatis.po.User">select * from user where username=#{username}</select><delete id="deleteUser" parameterType="java.lang.String">delete from user where username=#{username}</delete></mapper>Finally, we need to create a new configuration file config.xml for Mybatis to connect to the database under src, and import the above userMapper.xml. The code 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><environments default="development"><environment id="development"><transactionManager type="JDBC" /><!-- Configure database connection information--><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/test" /><property name="username" value="root" /><property name="password" value="" /></dataSource></environment></environments><mappers><!-- Register the userMapper.xml file, resource in the directory where userMapper.xml is located--><mapper resource="com/mybatis/mapping/userMapper.xml"/></mappers></configuration>
The configuration database connection information here is not very different from Hibernate. Now let’s create a new Test class to test:
public class Test {public static void main(String[] args) throws IOException {//mybatis configuration file String resource = "config.xml";//Use class loader to load mybatis configuration file (it also loads the associated mapping file) InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);//Build the factory of sqlSessionSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);//Open sessionSqlSession session = sessionFactory.openSession();/*** The identification string for mapping sql *com.mybatis.mapping.userMapper is the value of the namespace attribute of the mapper tag in the userMapper.xml file. * getUser is the id attribute value of the select tag. The SQL to be executed can be found through the id attribute value of the select tag. */String statement = "com.mybatis.mapping.userMapper.getUser";//Mapping sql string//Execute the query to return a unique user object sqlUser user = session.selectOne(statement,"username1");System.out.println(user.getUsername());String statement2="com.mybatis.mapping.userMapper.deleteUser";session.delete(statement2,user);}}Execute the selectOne method to return a user object (if you want to query multiple pieces of data, you can use selectList, which will return the object of List<User>). We output the username of the user object in the console. Execute the delete method to directly delete the corresponding data of the object. You can judge whether the execution is successful based on the changes in the database. Below is the directory of my project, you can refer to it:
The above is a simple example of Mybatis. Of course, in userMapper.xml, we can also use OGNL to generate dynamic SQL statements. Those who are interested can study it yourself. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!