首页 > 开发 > 综合 > 正文

mybatis快速入门

2024-07-21 02:52:26
字体:
来源:转载
供稿:网友

1,建工程,导入jar包:

mybatis-3.4.1.jarMySQL-connector-java-6.0.5-bin.jar

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,添加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?serverTimezone=UTC" /><property name="username" value="root" /><property name="passWord" value="123456" /></dataSource></environment></environments><mappers><mapper resource="com/zhuyun/mybatis/userMapper.xml" /></mappers></configuration>

4,新建User实体类

package com.zhuyun.mybatis;public class User {	private int id;	private String name;	private int age;	public User() {		super();		// TODO Auto-generated constructor stub	}		public User(int id, String name, int age) {		super();		this.id = id;		this.name = name;		this.age = 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 + "]";	}	}

5,创建user表的sql映射文件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"><mapper namespace="com.zhuyun.mybatis.userMapper">		<insert id="addUser" parameterType="User">		insert into users(name,age) values(#{name}, #{age})	</insert>		<delete id="deleteUser" parameterType="int">		delete from users where id=#{id}	</delete>		<update id="updateUser" parameterType="User">		update users set name=#{name},age=#{age} where id=#{id}	</update>	<select id="getUser" parameterType="int" resultType="User">		select * from users where id = #{id}	</select>		<select id="getAllUsers" resultType="User">		select * from users	</select></mapper>

6, 新建Test测试类

package com.zhuyun.mybatis;import java.io.InputStream;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class Test {	public static void main(String[] args) throws Exception {		InputStream is = Test.class.getClassLoader().getResourceAsStream("conf.xml");		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);		//设置成自动提交,默认手动提交		SqlSession session = sessionFactory.openSession(true);				String statement = null;				//添加		statement = "com.zhuyun.mybatis.userMapper.addUser";		int insert = session.insert(statement, new User(-1, "infi", 28));		System.out.println(insert);				//删除		statement = "com.zhuyun.mybatis.userMapper.deleteUser";		int delete = session.delete(statement, 9);		System.out.println(delete);				//查询		statement = "com.zhuyun.mybatis.userMapper.getUser";		User user = session.selectOne(statement,1);		System.out.println(user);				//修改		statement = "com.zhuyun.mybatis.userMapper.updateUser";		int update = session.update(statement, new User(10, "yinfei", 30));		System.out.println(update);				//查询所有		statement = "com.zhuyun.mybatis.userMapper.getAllUsers";		List<User> list = session.selectList(statement);		System.out.println(list);				session.close();	}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表