Mybatis入门(一)——单条数据查询
MyBatis入门(一)—单条数据查询
准备:
mybatis-3.4.1.jar
mysql-connector-java-5.1.26-bin.jar
1.创建普通java项目,导入jar包
2.建表
CREATE TABLE `student` ( `Sno` varchar(7) NOT NULL, `Sname` varchar(10) NOT NULL, `Ssex` char(4) DEFAULT NULL, `Sage` varchar(11) DEFAULT NULL, `Sdept` varchar(20) DEFAULT '计算机系', `c_id` varchar(30) DEFAULT NULL, PRIMARY KEY (`Sno`) )
3.创建配置文件
config.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"> <configuration> <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://127.0.0.1/test"/> <property name="username" value="root"/> <property name="password" value="1234"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/bean/StudentInforMapping.xml"/> </mappers> </configuration>
4.新建StudentBean.java文件
Student.java
package com.bean; public class Student { private String sno; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public String getsSex() { return sSex; } public void setsSex(String sSex) { this.sSex = sSex; } public String getsAge() { return sAge; } public void setsAge(String sAge) { this.sAge = sAge; } public String getsDept() { return sDept; } public void setsDept(String sDept) { this.sDept = sDept; } public String sName; public String sSex; public String sAge; public String sDept; public String getcId() { return cId; } public void setcId(String cId) { this.cId = cId; } private String cId; public Student(String sno, String sName, String sSex, String sAge, String sDept,String cId) { super(); this.sno = sno; this.sName = sName; this.sSex = sSex; this.sAge = sAge; this.sDept = sDept; this.cId=cId; } public Student() { super(); } }
5.新建StudentMapper.xml文件
studentMapping.xml
<?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="com.bean.StudentMapping"> <select id="getStudent" parameterType="String" resultType="com.bean.Student"> select * from student where sno = #{sno} </select> </mapper>
参数解释:
namespace:唯一标示一个mapper.
parameterType:参数类型。
resultType:返回结果类型(反射思想)。
6.获取数据
package com; 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; import com.bean.Student; public class Main { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String xmlPath="config-mybatis.xml"; Reader reader=Resources.getResourceAsReader(xmlPath); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession=sqlSessionFactory.openSession(); String statement="com.bean.StudentInforMapping"+".getStudent"; Student studentInfor=sqlSession.selectOne(statement, "111"); System.out.println(studentInfor.getsName()); } }