MyBatis笔记(一)
MyBatis学习随笔
第一个MyBatis项目
- 创建项目
- 导入maven依赖,根据需要选择性添加mysql/oracle,spring,spring-mybatis等依赖,这里就不一一列出了
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency>
- 创建mybatis配置文件
<?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> <!--加载外部配置文件--> <properties resource="mysql8.properties"></properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!-- 这里的value可以使用常量 同时可以使用${}在配置文件中读取 后期整合spring就不需在此配置 --> <property name="driver" value="${driverClassName}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <!--这里拷贝加载的mapper文件的全路径--> <mapper resource="com/offcn/mapper/CustomerMapper.xml"/> </mappers> </configuration>
- 创建mapper的配置文件
<?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.offcn.mapper.CustomerMapper"> <!--根据cust_id查询客户--> <select id="selectCustomerById" parameterType="Int" resultType="com.offcn.bean.Customer"> SELECT * FROM `customer` WHERE cust_id = #{cust_id} </select> </mapper>
- 创建mapper.xml对应的接口,该接口名要和mapper文件名完全一致
-
package com.offcn.mapper; import com.offcn.bean.Customer; import org.springframework.stereotype.Repository; @Repository public interface CustomerMapper { public Customer selectCustomerById(Integer id); }
- Bean对象代码
-
package com.offcn.bean; import lombok.Getter; import lombok.Setter; import lombok.ToString; import org.springframework.stereotype.Component; /** * @author 张瑞丰 * @description 客户表bean * @date 2019/4/19 */ @Getter@Setter@ToString@Component public class Customer { private Integer custId; private String custName; private String custProfession; private String custPhone; private String email; }
测试类代码
-
@Test public void test() throws IOException { //创建SqlSessionFactoryBuilder SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); //读取配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml"); //获取session工厂 SqlSessionFactory sessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream); //获取会话 SqlSession sqlSession = sessionFactory.openSession(); //执行sql,并得到返回值 Customer result = (Customer)sqlSession.selectOne("selectCustomerById", 2); //获取结果 System.out.println(result); }
输出结果:Customer(custId=null, custName=null, custProfession=null, custPhone=null, email=libai@163.com)
- 只有email正常输出,其余字段都未正常输出,原因:表中字段除email外,名为cust_xx而bean则为custXx,因列名和字段名不一致导致,解决方案如下:
- 在mapper中创建resultMap将表中字段和bean的属性进行映射,同时要修改select的ResultMap为其id,一定不能打错,打错会报一万个错误
- 在mybatis全局配置文件中开启驼峰转下划线
- 在select语句中给列名起别名的形式
-
<?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.offcn.mapper.CustomerMapper"> <!--映射表字段和列名 type为bean的全类名--> <resultMap id="BaseResultMap" type="com.offcn.bean.Customer"> <id column="cust_id" property="custId"></id> <result column="cust_name" property="custName"></result> <result column="cust_profession" property="custProfession"></result> <result column="cust_phone" property="custPhone"></result> <result column="email" property="email"></result> </resultMap> <!--根据cust_id查询客户--> <select id="selectCustomerById" parameterType="Int" resultMap="BaseResultMap"> SELECT * FROM `customer` WHERE cust_id = #{cust_id} </select> </mapper>