MYbatis入门
今天学习了Mybatis,说一下自己的心得
1、Mybatis 入门
MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索封装。MyBatis 可以使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJO(Plain Old Java Objects,普通的 Java 对象)映射成数据库中的记录. 半自动化框架。必须写sql语句。
2、idea使用mybatis
2、1所需的jar包
【mybatis】
【MYSQL 驱动包】
【lombok】实体类不需要创建构造,get/set,toSpring方法,挺方便的
2、2数据库建表
create database mybatis;
use mybatis;
CREATE TABLE users(id INT PRIMARY KEY AUTO_INCREMENT, username
VARCHAR(20), password VARCHAR(20);
INSERT INTO users(username, password) VALUES('张三','123');
INSERT INTO users(username, password) VALUES('李四', '123');
2、3创建Mybatis的配置文件mybatis-conf.xml
一般我会把关于sql的归到mapper文件夹
<?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:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
</configuration>
2、4定义表所对应的实体类
我这里使用了lombok
@Data //生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。
@AllArgsConstructor //生成全参的构造方法。
@NoArgsConstructor //生成无参的构造方法。
public class User {
private int id;
private String name;
private int age;
}
2、5注入依赖pom.xml
我用的是maven项目使用mybatis
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mybatis01</artifactId>
<version>1.0-SNAPSHOT</version>
<!--mysql依赖-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--Lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
</dependencies>
</project>
2、5定义操作user表的sql映射文件mapper.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.aaa.qy129.dao.UserDao">
<select id="showAllUser" resultType="com.aaa.qy129.entity.User">
select * from user
</select>
</mapper>
2、6在conf.xml文件中注册mapper.xml文件
<mappers>
<mapper resource="mapper/mapper.xml"></mapper>
</mappers>
2、7编写测试代码:执行定义的select语句
可能会报main方法不可用,用@Test测试就可以了
//1,读取mybatis-config.xml配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
//2.获取sqlsessionfactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//3.获取sqlsession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.获取到dao的字节码文件
UserDao mapper = sqlSession.getMapper(UserDao.class);
//查询
List<User> list1 = sqlSession.selectList("com.aaa.qy129.dao.UserDao.showAllUser");
System.out.println(list1);
3、实际开发会在DAO层中方法。
3、1创建UserDao
public interface UserDao {
/**
* 查询所有用户
*/
List<Map<String ,Object>> showAllUser();
/**
* 插入一条新数据
*/
int insertUser(String username,String password);
/**
* 修改数据
*/
int update(User user);
/**
* 删除数据
*/
int delete(int id);
}
3、2定义mapper.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.aaa.qy129.dao.UserDao">
<select id="showAllUser" resultType="com.aaa.qy129.entity.User">
select * from user
</select>
<insert id="insertUser" >
insert into user(username,password) values(#{param1},#{param2})
</insert>
<update id="update">
update user set username=#{username},password=#{password} where id = #{id}
</update>
<delete id="delete">
delete from user where id = #{id}
</delete>
</mapper>
3、3在conf.xml注册mapper.xml
<mappers>
<mapper resource="mapper/mapper.xml"></mapper>
</mappers>
3、4创建测试类测试代码可行性
public class test {
public static void main(String[] args) throws Exception{
//1,读取mybatis-config.xml配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
//2.获取sqlsessionfactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//3.获取sqlsession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//4.执行sql
UserDao mapper = sqlSession.getMapper(UserDao.class);
/*//删除
int i = mapper.delete(6);
sqlSession.commit();
System.out.println(i);*/
//修改
/*int i = mapper.update(new User(1, "张三", "123"));
sqlSession.commit();
System.out.println(i);*/
//新增
/*int user = mapper.insertUser("李四", "123");
//事务提交
sqlSession.commit();
System.out.println(user);*/
//查询
List<Map<String, Object>> list = mapper.showAllUser();
System.out.println(list);
}
}