1.Mybatis数据查询步骤
2.在mappers映射器下创建文件(src/main/resources/mappers/goods.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="goods"> <select id="selectAll" resultType="com.imooc.mybatis.entity.Goods" > select * from t_goods order by goods_id desc limit 10 </select> </mapper>
3.在mybatis-config.xml中对goods.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> <!-- 环境,可以配置多个,default:指定采用哪个环境 --> <environments default="dev"> <!-- id:唯一标识 --> <environment id="dev"> <!--采用JDBC进行事务的管理commit/rollback--> <transactionManager type="JDBC"/> <!-- 数据源,连接池类型的数据源 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/babytun?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="197366"/> </dataSource> </environment> </environments> <!--声明mapper的配置文件--> <mappers> <mapper resource="mappers/goods.xml"/> </mappers> </configuration>
4.测试语句
/** * select查询语句执行 * * @throws Exception */ @Test public void testSelectAll() throws Exception { SqlSession session = null; try { session = MyBatisUtils.openSession(); List<Goods> list = session.selectList("goods.selectAll");//goods.selectAll 命名空间.(SQL语句的ID) for (Goods g : list) { System.out.println(g.getTitle()); } } catch (Exception e) { throw e; } finally { MyBatisUtils.closeSession(session); } }
5.在mybatis-config.xml中开启驼峰命名转换
<settings> <!-- goods_id ==> goodsId 驼峰命名转换 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
6.CRUD标签(select标签)
select – 书写查询sql语句 select中的几个属性说明: id属性:当前名称空间下的statement的唯一标识。必须。要求id和mapper接口中的方法的名字一致。 resultType:将结果集映射为java的对象类型。必须(和 resultMap 二选一) parameterType:传入参数类型。可以省略
标签:xml,语句,goods,查询,010,session,Mybatis,select From: https://www.cnblogs.com/LLL0617/p/16849156.html