第一个 Mybatis 程序
作者:互联网
1、第一个 Mybatis 程序
思路:搭建环境 --> 导入 Mybatis --> 编写代码 --> 测试
1.1、搭建环境
-
搭建数据库
CREATE DATABASE `mybatis`;
USE `mybatis`;
CREATE TABLE `user`(
`id` INT(20) NOT NULL PRIMARY KEY,
`name` VARCHAR(30) DEFAULT NULL,
`pwd` VARCHAR(30) DEFAULT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(`id`,`name`,`pwd`)
VALUES(1,'张三','123456'),(2,'李四','123456');
-
导入 Maven 依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
1.2、创建一个模块
-
编写一个 Mybatis 核心配置文件
<?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?
useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/wuli/dao/userMapper.xml"/>
</mappers>
</configuration>
-
编写 Mybatis 工具类
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用Mybatis第一步,获取SqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
1.3、编写代码
-
实体类
private int id;
private String name;
private String pwd; -
Dao 接口
List<User> getUserList();
-
接口实现类由原来的 UserDaoImpl 转变为一个 Mapper 配置文件
<?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.wuli.dao.UserDao">
<select id="getUserList" resultType="com.wuli.pojo.User">
select * from mybatis.user
</select>
</mapper> -
测试
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
//关闭sqlSession
sqlSession.close();
}
Maven 由于他的约定大于配置,如果玉带我们写的配置文件无法被导出或者生效的问题,解决方案如下:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
2、CRUD
-
编写接口
-
编写对应的mapper中的sql语句
-
测试
2.1、namespace
配置文件中namespace中的名称为对应Mapper接口或者Dao接口的完整包名,必须一致
2.2、select
选择,查询语句:
-
id:就是对应的namespace中的方法名
-
resultType:SQL 语句执行的返回值
-
parameterType:参数类型
<select id="getUserList" resultType="com.wuli.pojo.User">
select *
from mybatis.user
</select>
<select id="getUserById" resultType="com.wuli.pojo.User" parameterType="int">
select *
from mybatis.user
where id = #{id}
</select>
2.3、insert
<insert id="addUser" parameterType="com.wuli.pojo.User">
insert into mybatis.user (`id`, `name`, `pwd`)
values (#{id}, #{name}, #{pwd})
</insert>
2.4、update
<update id="updateUser" parameterType="com.wuli.pojo.User">
update mybatis.user
set name=#{name},
pwd=#{pwd}
where id = #{id}
</update>
2.5、delete
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id}
</delete>
注意点:增删改需要提交事务
标签:第一个,程序,mybatis,pwd,user,Mybatis,id,name 来源: https://www.cnblogs.com/is-wgy/p/16526014.html