Mybatis Learning (1), Mybatis Learning (
1) import the pom file first
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Mybatis</groupId> <artifactId>com.mybatis</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>com.mybatis Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!-- mysql-connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>com.mybatis</finalName> </build></project>
View Code2) create a configuration file
3) database driver file:
4) Configure mybatis. cfg. xml for 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> <! -- Introduce the external configuration file --> <properties resource = "config. properties"> </properties> <typeAliases> <! -- Alias method 1: the full path of the class is placed in one configuration type, and the class name <typeAliase type = "com. cy. mybatis. beans. userBean "alias =" UserBean "/> --> <! -- Alias method 2: automatically scans and uses the JAVA class name as the class name --> <package name = "com. mybatis. entity"/> </typeAliases> <! -- Configure the mybatis runtime environment --> <environments default = "cybatis"> <environment id = "cybatis"> <! -- Type = "JDBC" indicates the use of JDBC commit and rollback to manage transactions --> <transactionManager type = "JDBC"/> <! -- Mybatis provides three data source types: POOLED, UNPOOLED, and JNDI --> <! -- POOLED indicates that JDBC data source connection pool is supported --> <! -- UNPOOLED indicates that the data source connection pool is not supported --> <! -- JNDI indicates that the external data source connection pool is supported --> <dataSource type = "POOLED"> <property name = "driver" value = "$ {jdbc. driver} "/> <property name =" url "value =" $ {jdbc. url} "/> <property name =" username "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> </dataSource> </environment> </environments> <mappers> <! -- Inform ing file method 1, one by one configuration <mapper resource = "com/cy/mybatis/mapper/UserMapper. xml "class =" com. mybatis. mapper. userMapper "/> --> <! -- Inform ing file method 2 to automatically scan the ER interface and configuration file in the package --> <package name = "com/mybatis/Mapper"/> </mappers> </configuration>5) Create a database table:
6) Compile the entity class:
package com.mybatis.entity;import java.io.Serializable;public class User implements Serializable{ private Integer id; private String username; private String password; private Double account; public User() { super(); } public User(Integer id, String username, String password, Double account) { super(); this.id = id; this.username = username; this.password = password; this.account = account; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Double getAccount() { return account; } public void setAccount(Double account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}View Code
7) write the ing data access interface:
package com.mybatis.mapper;import com.mybatis.entity.User;public interface UserMapper { public int saveUser(User user); public int updateUser(User user); public int deleteUser(User user); public int selectUserById(int id); public int selectAllUser(); }View Code
8) configure the data access interface ing xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.mybatis.mapper.UserMapper"> <resultMap type="User" id="user"> <id property="id" column="id" javaType="java.lang.Integer"/> <result property="username" column="username" javaType="java.lang.String"/> <result property="password" column="password" javaType="java.lang.String"/> <result property="account" column="account" javaType="java.lang.Double"/> </resultMap> <insert id="saveUser" useGeneratedKeys="true" keyProperty="id"> insert into t_user(username,password,account) values(#{username},#{password},#{account}) </insert></mapper>
9) because it is not integrated with spring, it is considered that the session is not closed when the transaction is committed. (note)
Package com. mybatis. tool; 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 DBTools {public static SqlSessionFactory sessionFactory; static {try {// use the Resources class provided by MyBatis to load the configuration file of mybatis Reader = Resources. getResourceAsReader ("mybatis. cfg. xml "); // The sqlSession factory sessionFactory = new SqlSessionFactoryBuilder (). build (reader);} catch (Exception e) {e. printStackTrace () ;}}// create a sqlSession public static SqlSession getSession () {return sessionFactory that can execute SQL in the SQL ing file. openSession ();}}View Code
10) test:
Package com. mybatis. service; import org. apache. ibatis. session. sqlSession; import com. mybatis. entity. user; import com. mybatis. mapper. userMapper; import com. mybatis. tool. DBTools; public class UserService {public void save (User user) {SqlSession session = DBTools. getSession (); int count = session. getMapper (UserMapper. class ). saveUser (user); session. commit ();} public static void main (String [] args) {User user = new User (null, "Binbin", "123456", 8.0); new UserService (). save (user );}}
Successful, completed in the first stage