新建数据表 create table tb_user( uid int , username varchar(50), password varchar(50), age int, sex varchar(50));
插入数据 INSERT INTO tb_user(uid) VALUES(1);
开始写Java代码,实现mybatis的简单运用 总体结构如下
写实体类,对应数据表中的字段
package com.zhu.bean; public class User { private Integer uid; private String userName; private String password; private Integer age; private String sex; /** * @return the uid */ public Integer getUid() { return uid; } /** * @param uid the uid to set */ public void setUid(Integer uid) { this.uid = uid; } /** * @return the userName */ public String getUserName() { return userName; } /** * @param userName the userName to set */ public void setUserName(String userName) { this.userName = userName; } /** * @return the password */ public String getPassword() { return password; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } /** * @return the age */ public Integer getAge() { return age; } /** * @param age the age to set */ public void setAge(Integer 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; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "User [uid=" + uid + ", userName=" + userName + ", password=" + password + ", age=" + age + ", sex=" + sex + ", getUid()=" + getUid() + ", getUserName()=" + getUserName() + ", getPassword()=" + getPassword() + ", getAge()=" + getAge() + ", getSex()=" + getSex() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]"; } }配置连接数据库的mybatis-config.xml,mapper里面的配置是引入一个对数据库操作语句的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="mysql"> <environment id="mysql"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test_mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!-- 引入SQL映射文件,Mapper映射文件 --> <mappers> <mapper resource="UserMapper.xml" /> </mappers> </configuration>定义一个接口
package com.zhu.mapper; import com.zhu.bean.User; public interface UserMapper { User getUserByUid(String uid); }定义操作语句的Usermapping.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.zhu.mapper.UserMapper"> <!-- <select>: 定义查询语句 id : 设置SQL语句的唯一标识,为借口的方法名 resultType: 结果类型,即实体类的全限定名 --> <select id="getUserByUid" resultType="com.zhu.bean.User"> select uid, username, password, age, sex from tb_user where uid = #{uid} </select> </mapper>测试代码是否正确
package com.zhu.mapper; import java.io.IOException; import java.io.InputStream; 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.junit.Test; import com.zhu.bean.User; import com.zhu.mapper.UserMapper; public class TestMybatis { @Test public void test() { try { //加载对数据库操作的配置文件 InputStream is=Resources.getResourceAsStream("mybatis-config.xml"); //通过配置信息来构建一个SqlSessionFactory SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is); //通过SqlSessionFactory打开一个会话 SqlSession sqlSession= sqlSessionFactory.openSession(true); //加载执行方法的抽象类 UserMapper mapper=sqlSession.getMapper(UserMapper.class); //执行类里面的方法 List<User> emp=mapper.getAllEmp(); System.out.print(emp); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }