首页
登录 | 注册

mybatis入门案例1

1环境准备

jdk1.7+eclilpse nuna+mybatis3.2.7+mysql

2工程搭建

导入jar包:

    1mybatis的核心包和依赖包

     2mysql的驱动包

     3junit(可选择)

选中所有jar包-右键-buildpath-add

mybatis入门案例1

3定义类User

package cn.itcast.po;

import java.util.Date;

public class User {
	private int id;
	private String username;
	private String sex;
	private Date birthday;
	private String address;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", sex=" + sex
				+ ", birthday=" + birthday + ", address=" + address + "]";
	}
	

}


4在config下创建SqlMapConfig.xm全局配置l文件

<?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>
<!-- 配置mybatis的环境信息 -->
<environments default="development">
	<environment id="development">
		<!-- 配置JDBC事务控制,由mybatis进行管理 -->
		<transactionManager type="JDBC"></transactionManager>
		<!-- 配置数据源,采用连接池 -->
		<dataSource type="POOLED">
			<property name="driver" value="com.mysql.jdbc.Driver"/>
			<property name="url" value="jdbc:mysql://localhost:3306/zn"/>
			<property name="username" value="root"/>
			<property name="password" value="123456"/>
		</dataSource>
	</environment>
</environments>
<!-- 加载映射文件 -->
<mappers>
	<mapper resource="User.xml"/>
</mappers>
</configuration>


5在config下创建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="test">
<select id="findUserById" parameterType="int" resultType="cn.itcast.po.User">
select * from user where id = #{id}
</select>
</mapper>


6在SqlMapConfig.xml中加载映射文件

<!-- 加载映射文件 -->
<mappers>
	<mapper resource="User.xml"/>
</mappers>


7测试代码

package cn.itcast.first;

import java.io.IOException;
import java.io.InputStream;

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 org.junit.Test;

import cn.itcast.po.User;

import com.sun.corba.se.spi.orbutil.fsm.Input;

public class MyBatisFirst {
	@Test
	public void findUserByIdTest() throws Exception{
		//读取配置文件
		String resource ="SqlMapConfig.xml";
		InputStream inputStream =  Resources.getResourceAsStream(resource);
		//创建SqlSessionFactory
		 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
		//创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//调用SqlSession的增删改查
		User user = sqlSession.selectOne("test.findUserById", 1);
		System.out.println(user);
		//关闭资源
		sqlSession.close();
	}

}


8测试

点击类名-右键-Run As-Junit




2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.008 s.
京ICP备10005923号