欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybaits——动态代理方式的增删改查(又称mybaits接口开发)

程序员文章站 2022-05-28 08:05:02
...

原则:约定优于配置,配置优于硬编码

目的:根绝约定直接找到sql语句,而不是通过mapper.xml的包名.类名+的id值定位sql语句

步骤:

1、建一个接口(在mybaits中,是没有接口实现类的)

     在接口中,方法名和mapper.xml标签中的id值保持一致;

    方法的输入参数和mapper.xml文件标签的parameterType类型一致(如果没有parameterType,则说明该方法没有输入参数);

    方法的输出参数和mapper.xml标签中的resultType类型保持一致;

除了以上的约定,要实现接口中的方法和mapper.xml中sql标签一一对应,还需一下两点:

a、 namespace的值,就是接口的全类名(接口  ----mapper.xml  意义对应;根据接口名找到mapper.xml文件)

b、根据接口的方法名找到mapper.xml文件中的sql标签

StudentMapper.java接口

public interface StudentMapper {
	/*约定的原则:
	 * 1、方法名和mapper.xml文件中标签的id值相同
	 * 2、方法的输入参数和mapper.xml文件标签的parameterType类型一致(如果没有parameterType,则说明该方法没有输入参数)
	 * 3、方法的返回值和mapper.xml文件标签的resultType类型一致*/
	 
	//增加
	void addStudent(Student student);
	//修改
	void updateStudentByStuno(Student student);
	//删除
	void deleteStudentByStuno(int stuno);
	//查询全部
	List<Student> queryAllStudents();
	
}

studengMapper.xml

<insert id="addStudentWithConverter" parameterType="com.maven.myBaits.two.Student">
    		insert into student (stuno,stuname,stuage,graname,stusex) values(#{stuNo},#{stuName},#{stuAge},#{graName},#{stuSex,javaType=boolean,jdbcType=INTEGER})
    </insert>
    
    
  
    <update id="updateStudentByStuno" parameterType="com.maven.myBaits.two.Student">
    update student set stuname=#{stuName},stuage=#{stuAge},graname=#{graName},stusex=#{stuSex} where stuno=#{stuNo}
    </update>
    
    <delete id="deleteStudentByStuno" parameterType="int">
    	delete from student where stuno=#{stuno}
    </delete>
    
    <select id="queryAllStudents" resultType="com.maven.myBaits.two.Student">
    	select * from student
    </select>

Test.java

//查询全部学生
		public static void queryAllStudents() throws IOException{
			
			//加载mybaits配置文件
			Reader reader=Resources.getResourceAsReader("conf.xml");
			
			SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
			SqlSession session=sessionFactory.openSession();
			
			StudentMapper studentMapper=session.getMapper(StudentMapper.class);
			List<Student> students=studentMapper.queryAllStudents();
			
			System.out.println(students);
			session.close();

		}
		//增加学生
		public static void addStudent() throws IOException{
			
			//加载mybaits配置文件
			Reader reader=Resources.getResourceAsReader("conf.xml");
			
			SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
			SqlSession session=sessionFactory.openSession();
			
			Student student=new Student(47,"ss",25,"s1",true);
			
			StudentMapper studentMapper=session.getMapper(StudentMapper.class);
			studentMapper.addStudent(student);
			
			//int count=session.insert(statement,student);//指定执行得sql;student是参数
			session.commit();//提交事物
			System.out.println("增加成功");
			session.close();

		}
		//删除学生
		public static void deleteStudentByStuno() throws IOException{
			
			//加载mybaits配置文件
			Reader reader=Resources.getResourceAsReader("conf.xml");
			
			SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
			
			SqlSession session=sessionFactory.openSession();
			
			StudentMapper studentMapper=session.getMapper(StudentMapper.class);
			studentMapper.deleteStudentByStuno(3);	
			//int count=session.delete(statement,123);
			
			session.commit();//提交事物
		    System.out.println("删除成功");
			session.close();
		}
		
		//修改学生
		public static void updateStudentByStuno() throws IOException{
			
			//加载mybaits配置文件
			Reader reader=Resources.getResourceAsReader("conf.xml");
			
			SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
			
			SqlSession session=sessionFactory.openSession();
			
			StudentMapper studentMapper=session.getMapper(StudentMapper.class);
			
			//修改的参数
			Student student = new Student();
			//修改哪个人,where stuno=1
			student.setStuNo(234);
			//修改成什么样子
			student.setStuName("lxs");
			student.setStuAge(44);
			student.setGraName("s2");
			//执行session
			//int count=session.update(statement,student);
			//提交事务
			
			studentMapper.updateStudentByStuno(student);
			
			session.commit();
			System.out.println("修改成功");
			session.close();
		}

	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub
		//queryStudentByStuno();
		//addStudent();
		//queryAllStudents();
		//deleteStudentByStuno();
		//updateStudentByStuno();
		//queryAllStudents();
		
		queryStudentByStunoWithConverter();
		//addStudent();
	}

 

session.getMapper()——动态代理方式

调用接口中的方法定位sql语句