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方式執行測試文件。

控制檯輸出:

 

 

 

 

查看數據庫:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章