MyBatis 3 MyBatis conventions based deletions and dynamics change search mode

MyBatis convention based deletions and dynamics change search mode

Learning to https://study.163.com/course/courseMain.htm?courseId=1005847005&_trace_c_p_k2_=82d8e4a53fbd48c5842d426aad41b6d2

Project structure

1.

CREATE TABLE empinfo (
eid INT (10) PRIMARY KEY,
name VARCHAR (20),
age INT(10),
sex VARCHAR(5)
);

2.

<?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">
<configuration>
	<!--通过 environments的 default值和environments的id值来指定MyBatis运行时的数据库环境-->
	 <environments default="development">
		 <environment id="development">
			 <transactionManager type="JDBC"/>
			 <dataSource type="POOLED">
			 	<!--修改的第一处,配置数据库信息,根据自己的数据库修改-->
				 <property name="driver" value="com.mysql.jdbc.Driver"/>
				 <property name="url" value="jdbc:mysql://localhost:3306/Company"/>
				 <property name="username" value="root"/>
				 <property name="password" value="root"/>
			 </dataSource>
	 	</environment>
	 </environments>
	 <mappers>
	 	<!--加载映射文件-->
	 	<mapper resource="org/lanqiao/mapper/empinfoMapper.xml"/><!--修改的第二处-->
	 </mappers>
</configuration>

3.

 

package org.lanqiao.entity;

public class Empinfo {
	private int eid;
	private String name;
	private int age;
	private String sex;
	private String phone;
	
	//无参构造
	public Empinfo () {
	}
	//有参构造
	public Empinfo (int eid, String name, int age, String sex, String phone) {
		super();
		this.eid = eid;
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.phone = phone;
	}
	
	/**
	 * @return the eid
	 */
	public int getEid() {
		return eid;
	}
	/**
	 * @param eid the eid to set
	 */
	public void setEid(int eid) {
		this.eid = eid;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}
	/**
	 * @return the sex
	 */
	public String getSex() {
		return sex;
	}
	/**
	 * @param sex the sex to set
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}
	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}
	/**
	 * @param phone the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return eid+"-"+name+"-"+age+"-"+sex+"-"+phone ;
	}

}

4.

 

<?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">
<mapper namespace="org.lanqiao.mapper.EmpinfoMapper">
	<!-- (1).按eid查询Empinfo -->
 	<select id="queryEmpinfoByeid" resultType="org.lanqiao.entity.Empinfo" parameterType="int">	
 	select* from empinfo where eid=#{eid}
 	</select>
 	<!-- (4).查询所有Empinfo -->
 	<select id="queryAllEmpinfos" resultType="org.lanqiao.entity.Empinfo">
 		select * from empinfo
 	</select>
 	<!-- (5).增加 -->
 	<insert id="addEmpinfo" parameterType="org.lanqiao.entity.Empinfo">
 		insert into empinfo(eid,name,age,sex,phone) values(#{eid},#{name},#{age},#{sex},#{phone})
 	</insert>
 	<!-- (6).删除 -->
 	<delete id="deleteEmpinfoByeid" parameterType="org.lanqiao.entity.Empinfo">
 		delete from empinfo where eid=#{eid}
 	</delete>
 	<!-- (7).修改 -->
 	<update id="updateEmpinfoByeid" parameterType="org.lanqiao.entity.Empinfo">
 		update empinfo set name=#{name},age=#{age},sex=#{sex} where eid=#{eid}
 	</update>
</mapper>

5.

package org.lanqiao.mapper;


import java.util.List;

import org.lanqiao.entity.Empinfo;

public interface EmpinfoMapper {
	/*
	 * 1.方法名和mapper.xml文件中标签的id值相同
	 * 2.方法的输入参数和mapper.xml文件中标签的parameterType类型一致
	 * 3.方法的返回值 和mapper.xml文件中标签的resultType类型一致
	 * 
	 */
	//(1).按eid查询Empinfo
	Empinfo queryEmpinfoByeid(int eid);
	//(4).查询所有Empinfo
	List<Empinfo> queryAllEmpinfos();
	//(5).增加
	void addEmpinfo(Empinfo empinfo);
	//(6).删除
	void deleteEmpinfoByeid(int eid);
	//(7).修改
	void updateEmpinfoByeid(Empinfo empinfo);
}

6.

package org.lanqiao.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.lanqiao.entity.Empinfo;
import org.lanqiao.mapper.EmpinfoMapper;

public class TestEmpinfo {
	//(1).按eid查询Empinfo
	public static void queryEmpinfoByeid() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		
		EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
		Empinfo empinfo=empinfoMapper.queryEmpinfoByeid(2017051133);
		System.out.println(empinfo);
		session.close();
	}
	//(4).查询所有Empinfo
	public static void queryAllEmpinfos() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		String statement = "org.lanqiao.entity.empinfoMapper."+"queryAllEmpinfos";		
		//List<Empinfo> empinfos = session.selectList(statement);
		EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
		List<Empinfo> empinfos=empinfoMapper.queryAllEmpinfos();
		System.out.println(empinfos);
		session.close();
	}
	//(5).增加
	public static void addEmpinfo() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		//String statement = "org.lanqiao.entity.empinfoMapper."+"addEmpinfo";
		Empinfo empinfo= new Empinfo(2017059,"ljq",23,"男",1211231123);
		//int count=session.insert(statement,empinfo);//statement:执行指定的SQL		student:sql中需要的参数(???)
		EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
		empinfoMapper.addEmpinfo(empinfo);
		session.commit();//提交事务
		System.out.print("增加成功;");
		System.out.println(empinfo);
		session.close();
	}
	//(6).删除
	public static void deleteEmpinfoByeid() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		//String statement = "org.lanqiao.entity.empinfoMapper."+"deleteEmpinfoByeid";
		//int count=session.delete(statement,20170519);
		EmpinfoMapper empinfoMapper =session.getMapper(EmpinfoMapper.class);
		empinfoMapper.deleteEmpinfoByeid(2017059);
		session.commit();//提交事务
		System.out.println("删除成功;");
		session.close();
	}
	//(7).修改
	public static void updateEmpinfoByeid() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		//String statement = "org.lanqiao.entity.empinfoMapper."+"updateEmpinfoByeid";
		Empinfo empinfo= new Empinfo();
		empinfo.setEid(2017059);					
		empinfo.setName("fy");
		empinfo.setAge(22);
		empinfo.setSex("男");
		empinfo.setPhone(1234567890);
		//int count=session.update(statement,empinfo);
		EmpinfoMapper empinfoMapper =session.getMapper(EmpinfoMapper.class);
		empinfoMapper.updateEmpinfoByeid(empinfo);
		session.commit();//提交事务
		System.out.print("修改成功;");
		System.out.println(empinfo);
		session.close();
	}
	public static void main(String[] args) throws IOException {
		queryEmpinfoByeid();//按eid查询单个Empinfo
		//queryAllEmpinfos();
		//addEmpinfo();
		//deleteEmpinfoByeid();
		//updateEmpinfoByeid();
	}
}

 

Published 35 original articles · won praise 5 · Views 844

Guess you like

Origin blog.csdn.net/m0_43443133/article/details/105277749