Beginners use mybatis
1. Mybatis ****
MyBatis is an excellent persistence layer framework that supports ordinary SQL queries , stored procedures and advanced mapping . MyBatis eliminates almost all manual settings of JDBC code and parameters, as well as retrieval and packaging of result sets. MyBatis can use simple XML or annotations for configuration and primitive mapping, mapping interfaces and Java POJOs (Plain Old Java Objects) into records in the database.
JDBC->dbutils->MyBatis->Hibernate
2. Mybatis quick start
Write the first test example based on mybaits :
2.1. Add jar package
【mybatis】
mybatis-3.1.1.jar
【MYSQL 驱动包】
mysql-connector-java-5.1.7-bin.jar
2.2. Build library + table
create database mybatis;
use mybatis;
CREATE TABLE users(id INT PRIMARY KEY AUTO_INCREMENT, NAME
VARCHAR(20), age INT);
INSERT INTO users(NAME, age) VALUES('Tom', 12);
INSERT INTO users(NAME, age) VALUES('Jack', 11);
2.3. Add Mybatis configuration file conf.xml
<?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" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
</configuration>
2.4. Define the entity class corresponding to the table
public class User {
private int id;
private String name;
private int age;
//get,set 方法
}
2.5. Define the SQL mapping file userMapper.xml for operating the users table
<?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.ykq.mybatis_test.test1.userMapper">
<select id="getUser" parameterType="int" resultType="com.ykq.mybatis_test.test1.User">
select * from users where id=#{id}
</select>
</mapper>
2.6. Register the userMapper.xml file in the conf.xml file
<mappers>
<mapper resource="com/ykq/mybatis_test/test1/userMapper.xml"/>
</mappers>
2.7. Write test code: execute the defined select statement
public class Test {
public static void main(String[] args) throws IOException {
String resource = "conf.xml";
//加载 mybatis 的配置文件(它也加载关联的映射文件)
Reader reader = Resources.getResourceAsReader(resource);
//构建 sqlSession 的工厂
SqlSessionFactory sessionFactory = new
SqlSessionFactoryBuilder().build(reader);
//创建能执行映射文件中 sql 的 sqlSession
SqlSession session = sessionFactory.openSession();
//映射 sql 的标识字符串
String statement = "com.atguigu.mybatis.bean.userMapper"+".selectUser";
//执行查询返回一个唯一 user 对象的 sql
User user = session.selectOne(statement, 1);
System.out.pritln(user);
}
}
Common mistakes
Solution: In mybatis-config, talk about the mapping file import mybatis-config.xml
mybatis garbled solution
appears this error
Solution : add the time zone after the database connection url in mybatis-config.xml
serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true
url: jdbc:mysql:``//localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true
Mybatis pass parameter correspondence problem
If the
parameters in the mybatis dao layer method of the mybatis insert statement are not written in the @Param annotation, the fields of the mapping file addition, deletion, and modification of the query statement can be one-to-one correspondence (the incoming parameter can be an object or a parameter),
if it is multiple parameters, You need the Param annotation, and the sql statement of the mapping file can be one-to-one correspondence