Mybatis tutorial 1: Build a Development Environment
Step 1:
Create a maven project and load the dependency in the pom File
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency></dependencies>
Step 2: Create a table in the MySQL database
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userName` varchar(50) DEFAULT NULL, `userAge` int(11) DEFAULT NULL, `userAddress` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;Insert INTO `user` VALUES ('1', 'summer', '100', 'shanghai,pudong');
Step 3: Create the configuration file configuration. xml of mybatis.
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE configuration PUBLIC "-// mybatis.org//DTD Config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration>
<! -- Define an alias --> <typeAliases> <typeAlias alias = "User" type = "com. yihaomen. mybatis. model. User"/> </typeAliases> <! -- Defines the database information. By default, the development database is used to build the environment --> <environments default = "development"> <environment id = "development">
<! -- Adopt JDBC Transaction Management --> <transactionManager type = "JDBC"/>
<! -- Configure database connection information --> <dataSource type = "POOLED"> <property name = "driver" value = "com. mysql. jdbc. driver "/> <property name =" url "value =" jdbc: mysql: // 127.0.0.1: 3306/mybatis-learn "/> <property name =" username "value =" root "/> <property name =" password "value =" tiger "/> </dataSource> </environment> </environments> <! -- Define --> <mappers> <mapper resource = "com/yihaomen/mybatis/model/User. xml"/> </mappers> </configuration>
Step 4: create a model
public class User { private int id; private String userName; private String userAge; private String userAddress;
setters & getters}
User. 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"><mapper namespace="com.yihaomen.mybatis.models.UserMapper"> <select id="selectUserByID" parameterType="int" resultType="User"> select * from `user` where id = #{id} </select></mapper>
The following explains the configuration files: Step 5: Test
import com.yihaomen.mybatis.model.User;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;import java.io.Reader;public class Test { private static SqlSessionFactory sqlSessionFactory; private static Reader reader; static { try { reader = Resources.getResourceAsReader("configuration.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } } public static SqlSessionFactory getSession() { return sqlSessionFactory; } public static void main(String[] args) { SqlSession session = sqlSessionFactory.openSession(); try { User user = (User) session.selectOne("com.yihaomen.mybatis.models.UserMapper.selectUserByID", 1); System.out.println(user.getUserAddress()); } finally { session.close(); } }}
Https://gitee.com/huayicompany/springmvc-mybatis