mybatis快速入门

1. 开发步骤

①添加MyBatis的坐标
②创建user数据表
③编写User实体类
④编写映射⽂件UserMapper.xml
⑤编写核⼼⽂件SqlMapConfig.xml
⑥编写测试类
 
2. 环境搭建
1)导⼊MyBatis的坐标和其他相关坐标
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <java.version>1.8</java.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!--mybatis坐标-->
<dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis</artifactId>
 <version>3.4.5</version>
</dependency>
<!--mysql驱动坐标-->
<dependency> 
 <groupId>mysql</groupId> 
 <artifactId>mysql-connector-java</artifactId> 
 <version>5.1.6</version> 
 <scope>runtime</scope>
</dependency>
<!--单元测试坐标-->
<dependency> 
 <groupId>junit</groupId> 
 <artifactId>junit</artifactId> 
 <version>4.12</version> 
 <scope>test</scope>
</dependency>
<!--⽇志坐标-->
<dependency> 
 <groupId>log4j</groupId> 
 <artifactId>log4j</artifactId> 
 <version>1.2.12</version>
</dependency>
2) 创建user数据表

 

3) 编写User实体
public class User { 
 private int id; 
 private String username; 
 private String password;
 //省略get个set⽅法
}
4)编写UserMapper映射⽂件
<?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="userMapper"> 
 <select id="findAll" resultType="com.lagou.domain.User"> 
 select * from User 
 </select>
</mapper>
5) 编写MyBatis核⼼⽂件
<!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:///test"/> 
 <property name="username" value="root"/>
 <property name="password" value="root"/> 
 </dataSource> 
 </environment> 
 </environments> 
 
 <mappers>
 <mapper resource="com/lagou/mapper/UserMapper.xml"/>
 </mappers>
</configuration>
6) 编写测试代码
//加载核⼼配置⽂件
InputStream resourceAsStream =
Resources.getResourceAsStream("SqlMapConfig.xml");
//获得sqlSession⼯⼚对象
SqlSessionFactory sqlSessionFactory = new 
 SqlSessionFactoryBuilder().build(resourceAsStream);
//获得sqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执⾏sql语句
List<User> userList = sqlSession.selectList("userMapper.findAll");
//打印结果
System.out.println(userList);
//释放资源
sqlSession.close();

3. MyBatis的增删改查操作

MyBatis的插⼊数据操作
1)编写UserMapper映射⽂件
<mapper namespace="userMapper">
<insert id="add" parameterType="com.lagou.domain.User">
insert into user values(#{id},#{username},#{password})
</insert>
</mapper>
2)编写插⼊实体User的代码
InputStream resourceAsStream =
Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
int insert = sqlSession.insert("userMapper.add", user);
System.out.println(insert);
//提交事务
sqlSession.commit();
sqlSession.close();
3)插⼊操作注意问题
• 插⼊语句使⽤insert标签
• 在映射⽂件中使⽤parameterType属性指定要插⼊的数据类型
•Sql语句中使⽤#{实体属性名}⽅式引⽤实体中的属性值•插⼊操作使⽤的API是sqlSession.insert(“命名空间.id”,实体对象);
•插⼊操作涉及数据库数据变化,所以要使⽤sqlSession对象显示的提交事务,即sqlSession.commit()
 
MyBatis的修改数据操作
1)编写UserMapper映射⽂件
<mapper namespace="userMapper">
<update id="update" parameterType="com.lagou.domain.User">
update user set username=#{username},password=#{password} where id=#
{id}
</update>
</mapper>
2)编写修改实体User的代码
InputStream resourceAsStream =
Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
int update = sqlSession.update("userMapper.update", user);
System.out.println(update);
sqlSession.commit();
sqlSession.close();
3)修改操作注意问题
• 修改语句使⽤update标签
• 修改操作使⽤的API是sqlSession.update(“命名空间.id”,实体对象);
 
MyBatis的删除数据操作
1)编写UserMapper映射⽂件
<mapper namespace="userMapper">
<delete id="delete" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>
</mapper>
2)编写删除数据的代码InputStream resourceAsStream =
Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
int delete = sqlSession.delete("userMapper.delete",3);
System.out.println(delete);
sqlSession.commit();
sqlSession.close();
3)删除操作注意问题
• 删除语句使⽤delete标签
•Sql语句中使⽤#{任意字符串}⽅式引⽤传递的单个参数
•删除操作使⽤的API是sqlSession.delete(“命名空间.id”,Object);
 
posted @ 2021-05-05 15:21  冰湖一角  阅读(58)  评论(0编辑  收藏  举报