1.添加mybatis配置文件conf.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="mybatisDemo"> <environment id="mybatisDemo"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <PRoperty name="driver" value="com.MySQL.jdbc.Driver" /> <property name="username" value="root" /> <property name="passWord" value="" /> <property name="url" value="jdbc:mysql:///test" /> </dataSource> </environment> </environments></configuration>2.定义表对应的实体类
package entity;public class User { private int id; private String name; private String pwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]"; }}3.定义操作t_user表的sql映射文件userMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 --><mapper namespace="mapper.userMapper"> <!-- 根据id查询user对象,参数类型为int --> <select id="getUser" parameterType="int" resultType="entity.User"> select id,name,password from t_user where id=#{id} </select></mapper>4、在conf.xml文件中注册userMapper.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="mybatisDemo"> <environment id="mybatisDemo"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="username" value="root" /> <property name="password" value="" /> <property name="url" value="jdbc:mysql:///test" /> </dataSource> </environment> </environments> <!-- 注册userMapper.xml文件 --> <mappers> <mapper resource="mapper/userMapper.xml" /> </mappers></configuration>5.编写测试类,执行select语句
package test;import java.io.IOException;import java.io.InputStream;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 entity.User;public class Test { public static void main(String[] args) throws IOException { // 使用类加载器加载conf.xml文件 InputStream in = Test.class.getClassLoader().getResourceAsStream("conf.xml"); // 创建SqlSessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in); /* * 使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件) Reader reader = * Resources.getResourceAsReader("conf.xml"); SqlSessionFactory * sessionFactory = new SqlSessionFactoryBuilder().build(reader); */ SqlSession session = sessionFactory.openSession(); User user = session.selectOne("mapper.userMapper.getUser", 1); System.out.println(user); }}查询结果
新闻热点
疑难解答