Mybatis项目笔记(一)简单创建mybatis项目

Mybatis项目笔记(一)简单创建mybatis项目

Myeclipse2016 +jre1.8+MySQL5.7

1 myeclipse进入创建java project

Mybatis项目笔记(一)简单创建mybatis项目_第1张图片

2 导入mybatis框架相关jar包:

需要用到的jar包先放置在项目目录下lib文件夹里边,没有此文件夹则新建

Mybatis项目笔记(一)简单创建mybatis项目_第2张图片

3 选择这些jar包,右击build path 选择Add to ….. :

Mybatis项目笔记(一)简单创建mybatis项目_第3张图片

4 在src目录下,创建mybatis配置文件(.xml文件)

Mybatis项目笔记(一)简单创建mybatis项目_第4张图片

5 从参考文档中复制示例配置到mybatis.xml再另外根据实际情况进行修改,Ctrl+A全选Ctrl+i格式化:


















 

6 准备数据库:t1数据库 goods商品表,goods_type商品类型表

Mybatis项目笔记(一)简单创建mybatis项目_第5张图片

7 将用于数据库连接的:driver、URL、username、password修改完整。


				
				
				
				
			

 

8 创建实体类,ORM映射

Mybatis项目笔记(一)简单创建mybatis项目_第6张图片

package com.mybatis.lzy.domain;

public class Goods {
	
//	goods_id	int
	private int goodsId;
//	goods_name	varchar
	private String goodsName;
//	goods_price	double
	private double goodsPrice;
//	goods_num	int
	private int goodsNum;
//	goods_type	int
	private int goodsType;
	public int getGoodsId() {
		return goodsId;
	}
	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}
	public String getGoodsName() {
		return goodsName;
	}
	public void setGoodsName(String goodsName) {
		this.goodsName = goodsName;
	}
	public double getGoodsPrice() {
		return goodsPrice;
	}
	public void setGoodsPrice(double goodsPrice) {
		this.goodsPrice = goodsPrice;
	}
	public int getGoodsNum() {
		return goodsNum;
	}
	public void setGoodsNum(int goodsNum) {
		this.goodsNum = goodsNum;
	}
	public int getGoodsType() {
		return goodsType;
	}
	public void setGoodsType(int goodsType) {
		this.goodsType = goodsType;
	}
	public Goods() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Goods(int goodsId, String goodsName, double goodsPrice,
			int goodsNum, int goodsType) {
		super();
		this.goodsId = goodsId;
		this.goodsName = goodsName;
		this.goodsPrice = goodsPrice;
		this.goodsNum = goodsNum;
		this.goodsType = goodsType;
	}
	@Override
	public String toString() {
		return "Goods [goodsId=" + goodsId + ", goodsName=" + goodsName
				+ ", goodsPrice=" + goodsPrice + ", goodsNum=" + goodsNum
				+ ", goodsType=" + goodsType + "]";
	}
	

}

9 创建mapper接口(映射器),定义想要实现的方法、函数

package com.mybatis.lzy.mapper;

import java.util.List;

import com.mybatis.lzy.VO.GoodsGroupVO;
import com.mybatis.lzy.VO.GoodsSearchVO;
import com.mybatis.lzy.domain.Goods;

public interface GoodsMapper {
//  查找所有商品
	public List findAll();
//  通过Id查找商品
	public Goods findById(int id);
//  插入商品
	public void save(Goods goods);
}

(另一个表的方法接口类似)

10 在与mapper接口同一包下创建XXXMaper.xml文件实现XXXMapper接口的方法,即sql语句会写在此配置文件里面





	

	

	
		insert into
		goods(goods_id,goods_name,goods_price,goods_num,goods_type)
		values(#{goodsId},#{goodsName},#{goodsPrice},#{goodsNum},#{goodsType})
	


	
		
		
		
		
		
	

12 同时记得注册XXXMapper文件:在mybatis.xml文件的标签中添加注册!注意路径使用“/”分开

Mybatis项目笔记(一)简单创建mybatis项目_第7张图片

11 编写测试类

package com.mybatis.lzy.test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

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

import com.mybatis.lzy.VO.GoodsGroupVO;
import com.mybatis.lzy.VO.GoodsSearchVO;
import com.mybatis.lzy.domain.Goods;
import com.mybatis.lzy.domain.GoodsType;
import com.mybatis.lzy.mapper.GoodsMapper;
import com.mybatis.lzy.mapper.GoodsTypeMapper;

public class MybatisTest {

	private SqlSession sqlsession;
	//	测试查找所有Goods
	public void findAllGoods(){
		System.out.println("是否进到该方法");
		try {
			GoodsMapper goodsMapper = this.sqlsession.getMapper(GoodsMapper.class);
			System.out.println(goodsMapper);
			List goodsList = goodsMapper.findAll();
			System.out.println("是否执行完查找所有");
			System.out.println(goodsList.size());
			for (Goods goods : goodsList) {
				System.out.println(goods);
			}
		} catch (Exception e) {
			sqlsession.close();
		}
	}
	//	测试查找所有GoodsType
	public void findAllGoodsType(){
		try {
			GoodsTypeMapper goodsTypeMapper = this.sqlsession.getMapper(GoodsTypeMapper.class);
			List goodsTypeList = goodsTypeMapper.findAll();
			for (GoodsType goodsTypes : goodsTypeList) {
				System.out.println(goodsTypes);
			}
		} catch (Exception e) {
			sqlsession.close();
		}
	}
	//	测试Goods findById
	public void findGoodsById(){
		try {
			GoodsMapper goodsMapper = this.sqlsession.getMapper(GoodsMapper.class);
			Goods good = goodsMapper.findById(3);
			System.out.println(good);
		} catch (Exception e) {

		}finally{
			sqlsession.close();
		}
	}
	//	插入商品Good
	public void saveGood(){
		//		创建一个Goods对象并赋值
		Goods goods = new Goods();
		goods.setGoodsId(2);
		goods.setGoodsName("测试save商品方法的商品A");
		goods.setGoodsNum(40);
		goods.setGoodsPrice(10);
		goods.setGoodsType(1);
		try {
			GoodsMapper goodsMapper = this.sqlsession.getMapper(GoodsMapper.class);
			goodsMapper.save(goods);
			//			提交事务
			this.sqlsession.commit();
			System.out.println("save成功插入一件商品");
		} catch (Exception e) {
			e.printStackTrace();
			this.sqlsession.rollback();
			throw new RuntimeException(e);
		}finally{
			//			释放资源
			this.sqlsession.close();
		}
	}

	@Test
//测试方法
	public void testF(){
		saveGood();
	}

	@Before
//放在测试方法执行前执行的before方法
	public void bofore(){
		String resource="mybatis.xml";
		try {
			Reader reader = Resources.getResourceAsReader(resource);
			SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
			//			System.out.println("获取前的sqlsession:" + sqlsession);
			sqlsession = ssf.openSession();
			//			System.out.println("获取后的sqlsession:" + sqlsession);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

 

你可能感兴趣的:(Java)