Mybatis初学创建一个MyBatis-Helloworld

版权声明:转载转载本博客文章必须声明出处,否则将追究其法律责任。 https://blog.csdn.net/vvcbvv/article/details/84292979

Mybatis初学创建一个MyBatis-Helloworld

1.首先建立一个bean类
根据数据库的字段建立好bean并为其创建get和set方法以及toString方法
2.建立一个测试类
测试类,相当于主函数在这里
3.导包
建立lib目录并且把这两个包buildpath一下

4.建立conf目录
(1)在conf目录下创建全局配置文件(mybatis-config.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://locallhost:3306/test"/>
 <property name="username" value="数据库帐号"/>
 <property name="password" value="数据库密码"/>
 </dataSource>
 </environment>
 </environments>
 <mappers>
 <mapper resource="EmployeeMapper.xml"/>
 </mappers>
</configuration>

(2)在conf目录下创建sql映射文件(EmployeeMapper.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.atguigu.mybatis.dao.EmployeeMapper">
<!-- 
	namespace:名称空间(指定为接口的全类名)
	id:唯一标识
	resultType:返回值类型(写bean的全类名)
 -->
	<select id="getEmployee" resultType="com.atguigu.mybatis.bean.Employee">
		select * from testtable where id = #{id}
	</select>
</mapper>

5.注意一定要将conf目录BuildPath一下
6.在测试类中写代码

1.根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象(目的为了创建SqlSession对象)
 2.获取sqlsession实例,能直接执行已经映射的sql语句

一. 第一种方法(老式)

void test() throws IOException {
		//1
		
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		
		//2(selectOne方法中的第一个参数为sql的唯一标识符(防止冲突:用namespace+id)、第二个为执行sql语句的参数)
		
		 SqlSession openSession = sqlSessionFactory.openSession();
		try {
			Employee employee = openSession.selectOne("com.atguigu.mybatis.EmployeeMapper.selectEmplo", 66);
			System.out.println("result:"+employee);
		}finally {
			openSession.close();
		}
		
	}

在第一种方法中selectOne方法中的第一个参数为sql的唯一标识符(防止冲突:用namespace+id【与sql映射文件EmployeeMapper.xml绑定】)、第二个为执行sql语句的参数(select * from testtable where id = #{id} 就是这条语句中#{id}的值)

二. 第二种方法(通过接口的方式,推荐使用第二种方法)

public void test01() throws IOException {
		//1.获取sqlsessionFactory
		SqlSessionFactory sqlSessionFactory = this.getSqlSessionFactory();
		//2.获取sqlsession
		SqlSession openSession = sqlSessionFactory.openSession();
		
		//3.获取接口的实现类对象
		try {
		EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
		Employee employee = mapper.getEmployee(66);
		System.out.println("通过接口的方式输出:"+employee);
		}finally {
			openSession.close();
		}
		
	}

因为每次都要new一个sqlSessionFactory所以我把它封装成一个方法

private SqlSessionFactory getSqlSessionFactory() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		return sqlSessionFactory;
	}

在第二种方法中需要创建一个接口并且与sql映射文件EmployeeMapper.xml动态绑定具体体现在
namespace:名称空间(指定为接口的全类名)
id:唯一标识(接口的方法名)

package com.atguigu.mybatis.dao;

import com.atguigu.mybatis.bean.Employee;

public interface EmployeeMapper {
	
	public Employee getEmployee(Integer id);
}

最终运行结果
运行结果
目录结构
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/vvcbvv/article/details/84292979