1.创建web项目,添加jar包
2.创建实验表user_t
3.在src下创建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="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/mi" /> <property name="username" value="root" /> <property name="password" value="dbdaocom" /> </dataSource> </environment> </environments> </configuration>
2、定义表所对应的实体类,
package com.mi.entity; public class User { private int id; private String userName; private String password; private Integer age; ... }
3、定义操作users表的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="com.mi.dao.UserInfoMapper"> <select id="queryUserInfo" resultType="com.mi.entity.User"> SELECT id,user_name userName,age FROM user_t where 1=1 </select> <select id="getCount" resultType="int"> SELECT count(*) FROM user_t </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="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/mi" /> <property name="username" value="root" /> <property name="password" value="dbdaocom" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/mi/mapping/UserInfoMapper.xml"/><!-- 注册userMapper.xml文件, --> </mappers> </configuration>
5、编写测试代码:执行定义的select语句
创建一个Test1类,编写如下的测试代码:
package com.mi.demo; import java.io.InputStream; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.mi.entity.User; public class Test1 { public static void main(String[] args) { //mybatis的配置文件 String resource = "conf.xml"; //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件) InputStream is = Test1.class.getClassLoader().getResourceAsStream(resource); //构建sqlSession的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件) //Reader reader = Resources.getResourceAsReader(resource); //构建sqlSession的工厂 //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //创建能执行映射文件中sql的sqlSession SqlSession session = sessionFactory.openSession(); /** * 映射sql的标识字符串, * com.mi.mapping.UserInfoMapper是UserInfoMapper.xml文件中mapper标签的namespace属性的值, * queryUserInfo是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL */ String statement = "com.mi.mapping.UserInfoMapper.queryUserInfo";//映射sql的标识字符串 User user = session.selectOne(statement,1);//执行查询返回一个唯一user对象的sql System.out.println(user.getId()); System.out.println(user.getUserName()); System.out.println(user.getAge()); } }
结果如下:
时间: 2024-12-18 03:58:14