MyBatis 第一个例子【学习笔记 二】

MyBatis 基础配置:https://blog.csdn.net/Kedongyu_/article/details/81543370

MyBatis第一个例子:

      1.修改MyBatis comfig.xml配置文件,改为自己想要连接的数据库

      2.创建数据库表

oracle版本:

--创建学生表
create table Student(
    stuNo number,
    stuName varchar2(16),
    phone varchar2(11)
);
--序列
create sequence student_seq;

Mysql版本,使用Mysql的话,刚刚配置文件config.xml需要修改数据库驱动:

create table Student(
    stuNo number AUTO_INCREMENT,
    stuName varchar(16),
    phone varchar(11)
);

      3.创建对应的Model类:StudentModel.class,包路径为:com.example.model

public class StudentModel {
	private int stuNo;
	private String stuName;
	private String phone;
	public int getStuNo() {
		return stuNo;
	}
	public void setStuNo(int stuNo) {
		this.stuNo = stuNo;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
}

      4.创建dao层接口,包路径为:com.example.dao

public interface IStudentDao {
	//增加
	public void create(StudentModel student) throws Exception; 
	//删除
	public void delete(StudentModel student) throws Exception; 
	//修改
	public void update(StudentModel student) throws Exception; 
	//查询-全部
	public List<StudentModel> selectListByAll() throws Exception; 
}

      5.创建映射文件IStudentDaoMapper.xml,包路径:com.example.dao.mapper

其中:

namespace填映射的dao层接口(全路径,按Ctrl+鼠标轻触路径,路径出现下划线则说明是对的,下同)

id指的是对应的dao层接口里面定义的方法。

parameterType指的是方法传入参数类型

resultType指的是方法返回结果类型

<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.IStudentDao">
	<insert id="create"	parameterType="com.example.model.StudentModel">
		insert into Student(StuNo,StuName,Phone)
		values(Student_SEQ.nextval,#{stuName},#{phone})
	</insert>
	<update id="update" parameterType="com.example.model.StudentModel">
		update Student set StuName=#{stuName}, Phone=#{phone}
		where StuNo=#{stuNo}
	</update>
	<delete id="delete" parameterType="com.example.model.StudentModel">
		delete from HT_Neighbourhood where StuNo=#{stuNo}
	</delete>
	<!-- 查询-全部 -->
	<select id="selectListByAll" resultType="com.example.model.StudentModel">
		select * from Student
	</select>
</mapper>

      6.在MyBatis配置文件config.xml指定映射文件。


	<mappers>
        <!-- 映射文件 -->
		<mapper	resource="com/example/dao/mapper/IStudentDaoMapper.xml"/>	
	</mappers>

      6.创建测试文件:StudentDaoTest.class,包路径:com.example.test。

public class StudentDaoTest {

	public static final void main(String[] args) throws Exception{
		String resource = "config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = sqlSessionFactory.openSession();
		IStudentDao studentDao=session.getMapper(IStudentDao.class);
		
		createTest(studentDao);
		session.commit();
		updateTest(studentDao);
		session.commit();
//		deleteTest(studentDao);
//		session.commit();
		toListByAllTest(studentDao);
		
		session.close();
	}
	
	public static void createTest(IStudentDao studentDao) throws Exception {
		StudentModel student=new StudentModel();
		student.setPhone("86110119120");
		student.setStuName("某某1号");
		studentDao.create(student);
		System.out.println("添加成功!");
	}
	public static void updateTest(IStudentDao studentDao) throws Exception{
		StudentModel student=new StudentModel();
		student.setPhone("86110119120");
		student.setStuName("某某2号");
		student.setStuNo(2);
		studentDao.create(student);
		System.out.println("修改成功!");
	}
	public static void deleteTest(IStudentDao studentDao) throws Exception{
		StudentModel student=new StudentModel();
		student.setStuNo(2);
		studentDao.delete(student);
		System.out.println("添加成功!");
	}
	public static void toListByAllTest(IStudentDao studentDao) throws Exception{
		List<StudentModel> list=studentDao.selectListByAll();
		System.out.println("查询成功!");
		for(StudentModel student:list) {
			System.out.println(student.getStuNo()+" "+student.getStuName()+" "+student.getPhone());
		}
	}
}

      6.以java Application方式执行测试文件。

控制台输出:

 

 

 

 

查看数据库:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值