Mybatis入门
1 Mybaits入门
1.1 需求列表
根据用户ID查询用户信息
根据用户名查找用户列表
添加用户
修改用户
删除用户
1.2 工程搭建
- 导入依赖jar包,在课前资源中有
- 配置SqlMapConfig.xml,可参考课前资料
- 配置log4j.properties,可参考课前资料
- 在课前资料复制pojo到工程目录下
- 配置sql查询的映射文件,可参考课前资料
- 加载映射文件
导入依赖jar包
配置SqlMapConfig.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> <!--和spring整合后environments配置将废除--> <environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理--> <transactionManager type="JDBC" /> <!-- 数据库连接池--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8" /> <property name="username" value="root" /> <property name="password" value="password" /> </dataSource> </environment> </environments> <!--加载映射文件--> <mappers> <mapper resource="mybatis/user.xml"></mapper> </mappers> </configuration>
配置log4j.properties
# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
创建pojo
package com.tanlei.newer.pojo; import java.util.Date; /** * @author:Mr.Tan * @Create:2018-10-28-07-28 **/ public class User { private int id; private String username;// 用户姓名 private String sex;// 性别 private Date birthday;// 生日 private String address;// 地址 public User() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", sex='" + sex + '\'' + ", birthday=" + birthday + ", address='" + address + '\'' + '}'; } }
配置sql查询的映射文件
<?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"> <!--namespace:命名空间,用于隔离sql语句,后继有重要--> <!--#{}相当于jdbc的?--> <mapper namespace="user"> <!--id:sql id ,语句的唯一标识 parameterType: 入参的数据类型 resultSetType: 返回结果的数据类型 --> <select id="getUserById" parameterType="int" resultType="com.tanlei.newer.pojo.User"> SELECT id,username,birthday,sex,address FROM user WHERE id=#{id2} </select> </mapper>
编写测试程序,加载映射文件
package com.tanlei.newer.test; import com.tanlei.newer.pojo.User; 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 java.io.IOException; import java.io.InputStream; /** * @author:Mr.Tan * @Create:2018-10-28-23-14 **/ public class MybatisTest { @Test public void testGetUserById() throws Exception { //创建SqlSessionFactoryBuilder对象 SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder(); //创建核心配置文件的输入流 InputStream inputStream=Resources.getResourceAsStream("SqlMapConfig.xml"); //通过输入流创建SqlSessionFactor对象 SqlSessionFactory sqlSessionFactor=ssfb.build(inputStream); //创建sqlSession对象 SqlSession sqlsession=sqlSessionFactor.openSession(); //执行查询 参数一:sql id 参数二:入参=parameterType="int" User user=sqlsession.selectOne("user.getUserById",10); //输出用户 System.out.println(user); //释放资源 sqlsession.close(); } }
需求完成步骤
- 编写sql语句
- 配置user映射文件
- 编写测试程序