MyBatis 框架之快速入门程序
Posted compassblog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis 框架之快速入门程序相关的知识,希望对你有一定的参考价值。
一、使用 IDEA 快速创建 Maven 项目
关于如何快速创建 Maven 项目,这个可以参考下面这篇文章:
二、快速配置 MyBatis 依赖 jar 包
打开 pom.xml
文件,快速配置 jar 包,配置代码如下:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mybatis</groupId>
<artifactId>MyBatisDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MyBatisDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- MyBatis 依赖包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- 数据库驱动依赖包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<!-- junit 测试依赖包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>MyBatisDemo</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
三、快速创建实体类
创建 pojo 类 User.java
,代码如下:
package com.mybatis.pojo;
public class User{
private int id;
private String username;
private String password;
private int age;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", age=" + age + "]";
}
}
四、快速创建实体类对应的数据表
新建数据库 mybatis_db,创建表 t_user,建表代码如下:
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘‘,
`age` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
五、快速创建 Mapper 接口
创建接口 UserDao.java
,代码如下:
package com.mybatis.dao;
import com.mybatis.pojo.User;
public interface UserDAO {
//新增用户
public int addUser(User user);
//根据id删除用户
public int deleteUser(int id);
//修改用户
public int updateUser(User user);
//根据id查询用户
public User selectUserById(int id);
}
六、快速创建 Mapper 接口代理文件
创建 xml 文件 UserDaoMapper.java
,代码如下:
<?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.mybatis.dao.UserDao">
<insert id="addUser" parameterType="com.mybatis.pojo.User">
insert into t_user (username,password,age) values (#{username},#{password},#{age})
</insert>
<delete id="deleteUser" parameterType="int">
delete from t_user where id=#{id}
</delete>
<update id="updateUser" >
update t_user set username=#{username},password=#{password},age=#{age} where id=#{id}
</update>
<select id="selectUserById" parameterType="int" resultType="com.mybatis.pojo.User">
select * from t_user where id=#{id}
</select>
</mapper>
七、快速添加 MyBatis 配置文件
创建 xml 文件 mybatis-comfig.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>
<!-- 配置 MyBatis 运行环境 -->
<environments default="development">
<environment id="development">
<!-- 配置 JDBC 事务管理 -->
<transactionManager type="JDBC" />
<!-- POOLED 配置 JDBC 数据源连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_db?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<!-- 注册 UserDAO.xml -->
<mappers>
<mapper resource="com/mybatis/dao/UserDaoMapper.xml"/>
</mappers>
</configuration>
八、快速创建测试类
创建测试类 Test.java
,代码如下:
package com.mybatis.test;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.mybatis.dao.UserDao;
import com.mybatis.pojo.User;
public class Test {
public static void main(String[] args) {
//加载MyBatis配置文件
InputStream is = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
//获取SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取实现接口的代理对象
UserDao userDAO = sqlSession.getMapper(UserDao.class);
//新增用户
User user = new User();
user.setUsername("孔乙己");
user.setPassword("123456");
user.setAge(21);
System.out.println(userDAO.addUser(user));
sqlSession.commit();
// //删除用户
// System.out.println(userDAO.deleteUser(1));
// sqlSession.commit();
//
// //查询用户
// User user1 = userDAO.selectUserById(1);
// System.out.println(user1);
//
// //修改用户
// User user2 = userDAO.selectUserById(1);
// user2.setUsername("指南者");
// System.out.println(userDAO.updateUser(user2));
// sqlSession.commit();
}
}
九、测试运行,结果如下图:
扫描二维码关注微信公众号,了解更多
-----------------------------------------
以上是关于MyBatis 框架之快速入门程序的主要内容,如果未能解决你的问题,请参考以下文章