欠失はMyBatisの2MyBatis・ステートメントを変更して再検索モード

欠失はMyBatisのステートメントを変更して再検索モード

プロジェクト構造

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">
			 	<!--修改的第一处,配置数据库信息,根据自己的数据库修改-->
			 	<!-- 使用mysql -->
				  <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/entity/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 int phone;
	
	//无参构造
	public Empinfo () {
	}
	//有参构造
	public Empinfo (int eid, String name, int age, String sex, int 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 int getPhone() {
		return phone;
	}
	/**
	 * @param phone the phone to set
	 */
	public void setPhone(int 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.entity.empinfoMapper">
	<!-- 一.(1)按eid查询
			(2)按name查询
			(3)按电话号码查询
			(4)查询所有员工的信息
	 	二.(5)增、(6)删、(7)改-->
	<!-- (1).按eid查询Empinfo -->
 	<select id="queryEmpinfoByeid" resultType="org.lanqiao.entity.Empinfo" parameterType="int">	
 	select* from empinfo where eid=#{eid}
 	</select>
 	<!-- (2).按name查询Empinfo -->
 	<select id="queryEmpinfoByname" resultType="org.lanqiao.entity.Empinfo" parameterType="String">	
 	select* from empinfo where name=#{name}
 	</select>
 	<!-- (3).按phone查询Empinfo -->
 	<select id="queryEmpinfoByphone" resultType="org.lanqiao.entity.Empinfo" parameterType="int">	
 	select* from empinfo where phone=#{phone}
 	</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.entity;

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;

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();
		String statement = "org.lanqiao.entity.empinfoMapper."+"queryEmpinfoByeid";
		Empinfo empinfo = session.selectOne(statement,2017051133);//eid=200812011
		System.out.println(empinfo);
		session.close();
	}
	//(2).按name查询Empinfo
	public static void queryEmpinfoByname() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		String statement = "org.lanqiao.entity.empinfoMapper."+"queryEmpinfoByname";
		Empinfo empinfo = session.selectOne(statement,"wyq");
		System.out.println(empinfo);
		session.close();
	}
	//(3).按phone查询Empinfo
	public static void queryEmpinfoByphone() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		String statement = "org.lanqiao.entity.empinfoMapper."+"queryEmpinfoByphone";
		Empinfo empinfo = session.selectOne(statement,1234567890);
		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);
		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(20170519,"ljq",23,"男",1211231123);
		int count=session.insert(statement,empinfo);//statement:执行指定的SQL		student:sql中需要的参数(???)
		session.commit();//提交事务
		System.out.print("增加了"+count+"个学生;");
		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);
		session.commit();//提交事务
		System.out.print("删除了"+count+"个学生;");
		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(2017051133);					
		empinfo.setName("fy");
		empinfo.setAge(24);
		empinfo.setSex("男");
		empinfo.setPhone(1234567890);
		//执行
		int count=session.update(statement,empinfo);
		session.commit();//提交事务
		System.out.print("修改了"+count+"个学生;");
		System.out.println(empinfo);
		session.close();
	}
	public static void main(String[] args) throws IOException {
		queryEmpinfoByeid();//按eid查询单个Empinfo
		//queryEmpinfoByname();//按name查询Empinfo
		//queryEmpinfoByphone();//按phone查询Empinfo
		//queryAllEmpinfos();//查询所有Empinfo
		//addEmpinfo();//(5).增加
		//deleteEmpinfoByeid();//(6).删除
		//updateEmpinfoByeid();//(7).修改
	}
}

 

公開された35元の記事 ウォンの賞賛5 ビュー845

おすすめ

転載: blog.csdn.net/m0_43443133/article/details/105277425