Mybatis(2)

昨天学了传统的写法,今天学习动态的,以下是相关笔记
mapper动态代理方式(接口开发)

原则:约定优于配置
配置:硬编码,abc.java/abc.mxl

eg:abc.java myProject
confi conf=new confi();
conf.setName(“myProject”);

约定:默认值就是myProject

具体步骤:
1.与之前相同,两个jar包,两个xml文件

2.约定的目标,省略掉statement,根据约定,直接定位出SQL语句

约定: * 方法名和mapper.xml中的id值相同
* 方法输入参数和mapper.xml文件中的parameterType一致
* 返回值就是resultType

除了以上约定,要实现接口中的方法和mapper.xml中的sql标签一一对应
namespace的值,就是接口的全类名(接口和mapper.mxl文件的映射)
过程:
接口名—>mapper.xml文件(根据namespace=接口全类名)
根据方法名–>mapper.xml文件的sql标签(方法名=Id的值)
我们调用接口的方法时,程序能定位到特定mapper文件的sql标签

习惯:sql映射文件(mapper.xml)和接口放在同一个包里 ,注意修改conf.xml中加载mapper.xml文件的位置

执行: StudentMapper studentmapper = Session.getMapper(StudentMapper.class);
Student student =studentmapper.queryStudentBystuno(1);//接口中的方法

方法:
用session对象获取接口 StudentMapper studentmapper = Session.getMapper(StudentMapper.class);
接口在调方法 studentmapper.addStudent(student);

优化:
1.
用properties.db存储相关数据
在conf.xml添加下列标签


2.mybatis全局参数设置(不要轻易去动)
在conf.xml

<settings>
	
	<setting name="" value=""/>
	
</settings>

3.设置别名(不区分大小写)
a.设置单独别名
b.设置多个别名

 <typeAliases>
 
 <!-- 
 单个别名 
 <typeAlias type="org.qgm.entity.Student" alias="Student"/>
 -->
 <!-- 多个别名 这个别名就是这个类本身,会批量定义-->
 <package name="org.qgm.entity"/>
 
 </typeAliases>

除了自定义的,mybatis内置了别的别名

类处理器之后再看。

新的conf.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">
  <!-- 上述两个的意思就是能在xml文件中提示各个参数,如果没有联网从project的properies中配置 -->
<configuration>
	<properties resource="db.properties">
	</properties>
	
<!-- 初学不要修改全局变量
	 <settings>
	
	<setting name="" value=""/>
	
	</settings>
 -->	
 <!-- 设置别名 -->
 <typeAliases>
 
 <!-- 
 单个别名 
 <typeAlias type="org.qgm.entity.Student" alias="Student"/>
 -->
 <!-- 多个别名 这个别名就是这个类本身,会批量定义-->
 <package name="org.qgm.entity"/>
 
 </typeAliases>
 <typeHandlers>
 <typeHandler handler="org.qgm.converter.BooleanAndInt" javaType="Boolean" jdbcType="int"/>
 </typeHandlers>
	<environments default="development"><!-- 用来选择在那个环境使用 -->
	<!-- 自己主机 -->
		<environment id="development">
			<transactionManager type="JDBC" />
			<!-- 
			事务提交方式:
			JDBC:手工方式:commit,rollback close
			MANAGED:将事务交给其他组件去托管(string),默认会关闭连接
			 -->
			<dataSource type="POOLED">
			<!--数据源类型:
			UNPOOLED:不适用数据连接池,使用jdbc
			POOLED:使用数据库连接池(第三方)
			JNDI:从tomcat中获取一个内置的数据库连接池(数据库连接池-数据源)
			连接池:是java代码与sql的一个连接集合,通过连接可以并发与去除打开或者关闭数据库的步骤
			 
			 -->
			
			
			<!-- 配置数据库信息 -->
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${username}" />
				<property name="password" value="${password}" />
			</dataSource>
		</environment>
		
		<environment id="shishi">
		<!-- 布置主机 -->
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
			<!-- 配置数据库信息 -->
				<property name="driver" value="com.mysql.cj.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3300/new?serverTimezone=UTC" />
				<property name="username" value="root" />
				<property name="password" value="123456" />
			</dataSource>
		</environment>
		<environment id="test">
		<!-- 測試主机 -->
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
			<!-- 配置数据库信息 -->
				<property name="driver" value="com.mysql.cj.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3300/new?serverTimezone=UTC" />
				<property name="username" value="root" />
				<property name="password" value="123456" />
			</dataSource>
		</environment>
		
	</environments>
	<mappers>
	<!-- 加载映射文件 -->
		<mapper resource="org/qgm/Mapper/studentMapper.xml" />
	</mappers>
</configuration>

主要添加vk对文件,与别名。转换器明天再看

新的mapper.mxl

<?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">
<!-- namespace:该Mapper.xml文件的唯一标识符 -->
<mapper namespace="org.qgm.Mapper.StudentMapper">
	<!-- id是用来唯一定位sql语句,parameterType是where后面要查找的类型,resulttype是*的返回值,*返回时类全部。 -->
	<select id="queryStudentBystuno" parameterType="int"
		resultType="Student">
		select * from student where stuNo=#{stuNo}

	</select>
	<insert id="addStudent" parameterType="Student">
		insert into
		student(stuNo,stuName,stuAge,graName)
		values(#{stuNo},#{stuName},#{stuAge},#{graName})

	</insert>

	<delete id="deleteStudentByStuno" parameterType="int">
		delete from student where stuNo=#{stuNo}
	</delete>

	<update id="updataStudentByStuno" parameterType="Student">
		update student set stuName=#{stuName},stuAge=#{stuAge},graName=#{graName} where stuNo=#{stuNo}

	</update>

	<select id="queryAllStudent" resultType="Student">
	select * from student 

	</select>




</mapper>


> 这是更改这之后指向studentmapper动态类
> 然后就加上了别名
<mapper namespace="org.qgm.Mapper.StudentMapper">

新增的mapper类

package org.qgm.Mapper;

import java.util.List;

import org.qgm.entity.Student;

//操作mybatis的接口
public interface StudentMapper {
	/*
	 * 方法名和mapper.xml中的id值相同
	 * 方法输入参数和mapper.xml文件中的parameterType一致
	 * 返回值就是resultType
	 */
	//public abstract Student queryStudentBystuno(int stuNo);接口不写也默认
	 Student queryStudentBystuno(int stuNo);
	 List<Student> queryAllStudent();
	 void addStudent(Student student);
	 void deleteStudentByStuno(int stuNo);
	 void updataStudentByStuno(Student student);
	 
}

新的Text测试文件

package org.qgm.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.qgm.Mapper.StudentMapper;
import org.qgm.entity.Student;

public class Test {
	// 查询单个
	public static void queryStudentByStuno() throws IOException {

		// 将一个文件读入reader流中,变成一个对象
		Reader reader = Resources.getResourceAsReader("conf.xml");
		// 将reader对象变成sqlsession对象
		SqlSessionFactory sqlsessionfactory = new SqlSessionFactoryBuilder().build(reader, "development");// 强制修改default的参数
		// 从工厂中取出
		SqlSession Session = sqlsessionfactory.openSession();

		// 由于之前在mapper中设置过了返回值对象,所以object对象即为student
		StudentMapper studentmapper = Session.getMapper(StudentMapper.class);
		Student student = studentmapper.queryStudentBystuno(7);// 接口中的方法
		System.out.println(student);
		Session.close();

	}

	// 查询全部学生
	public static void queryAllStudentBystuno() throws IOException {
		// 将一个文件读入reader流中,变成一个对象
		Reader reader = Resources.getResourceAsReader("conf.xml");
		// 将reader对象变成sqlsession对象
		SqlSessionFactory sqlsessionfactory = new SqlSessionFactoryBuilder().build(reader, "development");// 强制修改default的参数
		// 从工厂中取出
		SqlSession Session = sqlsessionfactory.openSession();
		StudentMapper studentmapper = Session.getMapper(StudentMapper.class);
		// 由于之前在mapper中设置过了返回值对象,所以object对象即为student
		List<Student> students = studentmapper.queryAllStudent();
		System.out.println(students);
		Session.close();

	}

	// 增加学生
	public static void addStudent() throws IOException {
		// 将一个文件读入reader流中,变成一个对象
		Reader reader = Resources.getResourceAsReader("conf.xml");
		// 将reader对象变成sqlsession对象
		SqlSessionFactory sqlsessionfactory = new SqlSessionFactoryBuilder().build(reader, "development");// 强制修改default的参数
		// 从工厂中取出
		SqlSession Session = sqlsessionfactory.openSession();
		StudentMapper studentmapper = Session.getMapper(StudentMapper.class);
		Student student = new Student(7, "sd", 48, "sdw");
		studentmapper.addStudent(student);
		System.out.println("增加成功");
		Session.close();

	}

	// 删除学生
	public static void deleteStudentBystuNo() throws IOException {

		// 将一个文件读入reader流中,变成一个对象
		Reader reader = Resources.getResourceAsReader("conf.xml");
		// 将reader对象变成sqlsession对象
		SqlSessionFactory sqlsessionfactory = new SqlSessionFactoryBuilder().build(reader, "development");// 强制修改default的参数
		// 从工厂中取出
		SqlSession Session = sqlsessionfactory.openSession();
		StudentMapper studentmapper=Session.getMapper(StudentMapper.class);
		studentmapper.deleteStudentByStuno(4);

		System.out.println("删除成功");
		Session.close();

	}

	// 修改
	public static void updateStudentBystuNo() throws IOException {

		// 将一个文件读入reader流中,变成一个对象
		Reader reader = Resources.getResourceAsReader("conf.xml");
		// 将reader对象变成sqlsession对象
		SqlSessionFactory sqlsessionfactory = new SqlSessionFactoryBuilder().build(reader, "development");// 强制修改default的参数
		// 从工厂中取出
		SqlSession Session = sqlsessionfactory.openSession();
		StudentMapper studentmapper=Session.getMapper(StudentMapper.class);
		Student student = new Student();
		// 修改谁
		student.setStuNo(2);
		// 改成啥
		student.setStuName("qwe");
		student.setStuAge(8488);
		student.setGraName("wdnmd");
		studentmapper.updataStudentByStuno(student);
		System.out.println("更新成功");
		Session.close();

	}

	public static void main(String[] args) throws IOException {
		queryStudentByStuno();

	}
}

新test文件中,StudentMapper studentmapper=session.getmapper(studentmapper.class)
然后students = studentmapper.queryAllStudent();调出函数

复习有关反射与el表达式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值