mybatis快速入门,mybatis简单实例, 如何使用mybatis
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis快速入门,mybatis简单实例, 如何使用mybatis相关的知识,希望对你有一定的参考价值。
目录结构:
1. 导入所需要的包
2. 创建数据库
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);
3. 建立相应的 javabean
package com.loger.bean; public class User { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } public User(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public User() { // TODO Auto-generated constructor stub } }
4. mybatis-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="apple" /> </dataSource> </environment> </environments> <!-- 注册 mapper --> <mappers> <mapper resource="mapper.xml"/> </mappers> </configuration>
5. 创建对数据库具体操作的 mapper.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"> <!-- namespace 是这个 mapper 的一个名称 --> <mapper namespace="userMapper"> <!-- 这是一个简单的查询操作, 操作的 id 是 getUser, parameterType 是查询所根据的参数 --> <select id="getUser" parameterType="int" resultType="com.loger.bean.User"> select * from users where id=#{id} </select> </mapper>
6. Main.java
package com.loger.test; import java.io.IOException; 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; import com.loger.bean.User; public class Main { public static void main(String[] args) throws IOException { //加载配置文件 Reader reader = Resources.getResourceAsReader("mybatis-conf.xml"); //构建 sqlSession 的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //使用该工厂 SqlSession session = sessionFactory.openSession(); //查询 String statement = "userMapper.getUser"; int id = 1; User user = session.selectOne(statement, id); System.out.println(user.toString()); } }
运行结果:
以上是关于mybatis快速入门,mybatis简单实例, 如何使用mybatis的主要内容,如果未能解决你的问题,请参考以下文章
13.2 MyBatis Generator 快速入门(MyBatis Generator逆向代码生成工具) -《SSM深入解析与项目实战》