MyBatis持久层框架的用法知识小结
MyBatis是一款优秀的持久化框架,通过XML或注解的方式实现了对象关系映射(ORM)。MyBatis主要解决了JDBC编程的繁琐和易错的问题,提供了诸如对象映射、缓存等一系列优秀的特性。下面将对MyBatis的使用进行详细介绍。
1. Maven依赖
在使用MyBatis前,需要在Maven项目中引入依赖。
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
2. 配置文件
MyBatis的配置文件位于src/main/resources目录下,文件名为mybatis-config.xml。在该配置文件中,需要配置两个主要内容:数据源和映射文件。
2.1 数据源配置
MyBatis可以支持多种数据源,如Druid、C3P0等。在数据源配置中,需要设置数据库连接驱动类、数据库连接URL、用户名和密码等信息。
<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/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
2.2 映射文件
在MyBatis中,映射文件主要用于描述SQL语句和Java对象之间的映射关系。映射文件使用XML格式编写,文件名以Mapper结尾,并且需要在mybatis-config.xml中配置。
<mappers>
<mapper resource="org/mapper/UserMapper.xml"></mapper>
</mappers>
3. SQL语句的编写
在MyBatis中,可以使用XML或注解的方式编写SQL语句。下面以XML方式为例,对MyBatis SQL语句的编写进行详细介绍。
3.1 增
<insert id="insertUser" parameterType="com.example.User">
insert into user (username, password)
values (#{username}, #{password})
</insert>
3.2 删
<delete id="deleteUserById" parameterType="int">
delete from user where id = #{id}
</delete>
3.3 改
<update id="updateUser" parameterType="com.example.User">
update user set password = #{password} where id = #{id}
</update>
3.4 查
<select id="selectUserById" parameterType="int" resultType="com.example.User">
select * from user where id = #{id}
</select>
4. Java接口的编写
MyBatis使用Java接口来描述SQL语句的执行过程。在Java接口中,需要定义方法名、参数类型和返回值类型。下面以增删改查为例,对Java接口的编写进行详细介绍。
4.1 增
public interface UserMapper {
public int insertUser(User user);
}
4.2 删
public interface UserMapper {
public int deleteUserById(int id);
}
4.3 改
public interface UserMapper {
public int updateUser(User user);
}
4.4 查
public interface UserMapper {
public User selectUserById(int id);
}
5. Java调用
在编写完Java接口后,需要在Java程序中调用接口中定义的方法。下面以增删改查为例,对Java调用的过程进行详细介绍。
5.1 增
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setUsername("张三");
user.setPassword("123456");
int result = userMapper.insertUser(user);
sqlSession.commit();
sqlSession.close();
5.2 删
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int result = userMapper.deleteUserById(1);
sqlSession.commit();
sqlSession.close();
5.3 改
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setId(1);
user.setPassword("123456");
int result = userMapper.updateUser(user);
sqlSession.commit();
sqlSession.close();
5.4 查
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.selectUserById(1);
sqlSession.close();
6. 示例
最后,给出一个完整的示例代码如下:
public class MyBatisDemo {
private SqlSessionFactory sqlSessionFactory;
public static void main(String[] args) {
MyBatisDemo mybatis = new MyBatisDemo();
// 初始化
mybatis.init();
// 增
User user = new User();
user.setUsername("张三");
user.setPassword("123456");
mybatis.insertUser(user);
// 删
mybatis.deleteUserById(1);
// 改
user.setPassword("654321");
mybatis.updateUser(user);
// 查
User result = mybatis.selectUserById(1);
System.out.println(result);
// 销毁
mybatis.destroy();
}
public void init() {
String resource = "mybatis-config.xml";
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public void destroy() {
sqlSessionFactory = null;
}
public int insertUser(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int result = userMapper.insertUser(user);
sqlSession.commit();
sqlSession.close();
return result;
}
public int deleteUserById(int id) {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int result = userMapper.deleteUserById(id);
sqlSession.commit();
sqlSession.close();
return result;
}
public int updateUser(User user) {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int result = userMapper.updateUser(user);
sqlSession.commit();
sqlSession.close();
return result;
}
public User selectUserById(int id) {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.selectUserById(id);
sqlSession.close();
return user;
}
}
结语
本文对MyBatis持久层框架的使用进行了详细介绍,并且给出了一些示例代码。MyBatis作为一款优秀的持久化框架,在实际开发中有着非常广泛的应用,希望本文能够对读者有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:MyBatis持久层框架的用法知识小结 - Python技术站