MyBatis 1MyBatis配置及入门

Mybatis配置及入门

学习于 https://study.163.com/course/courseLearn.htm?courseId=1005847005#/learn/video?lessonId=1053169135&courseId=1005847005

项目结构

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.

<?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">
	<!-- 按eid查询Empinfo -->
 	<select id="queryEmpinfoByeid" resultType="org.lanqiao.entity.Empinfo" parameterType="int">	
 	select* from empinfo where eid=#{eid}
 	</select>
</mapper>

4.

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 ;
	}

}

5.

package org.lanqiao.entity;

import java.io.IOException;

import java.io.Reader;

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 {
	public static void main(String[] args) throws IOException {
		//按eid查询Empinfo
		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);
		System.out.println(empinfo);
		session.close();
	}
}

6.结果

发布了35 篇原创文章 · 获赞 5 · 访问量 851

猜你喜欢

转载自blog.csdn.net/m0_43443133/article/details/105241326