MyBatis01:第一个程序
作者:互联网
MyBatis01:第一个程序
思路流程:搭建环境-->导入Mybatis--->编写代码--->测试
- 数据库文件
DROP DATABASE `mybatis`;
CREATE DATABASE `mybatis`;
USE `mybatis`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` INT(20) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`pwd` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(`id`,`name`,`pwd`)
VALUES (1,'小明','123456'),
(2,'张三','abcdef'),
(3,'李四','987654');
-
新建空白maven项目
-
pom.xml中导入jar包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
-
mybatis-config.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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <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"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> </configuration>
-
MybatisUtils工具类,使用SqlSessionFactory创建session,注意import的时候是ibatis包下的类
public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { String resource="mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream); }catch (IOException e){ e.printStackTrace(); } } //获取sqlession连接 public static SqlSession getSession(){ return sqlSessionFactory.openSession(); } }
-
实体类User
public class User {
private int id;
private String name;
private String pwd;
//构造,有参,无参
//set/get
//toString()
}
- UserMapper接口,声明一个查询所有用户的方法
public interface UserMapper {
List<User> selectUser();
}
- UserMapper.xml配置文件写sql,注意namespace
<?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.wgg.dao.UserMapper">
<select id="selectUser" resultType="com.wgg.pojo.User">
select * from user
</select>
</mapper>
- mybatis-config.xml中注册UserMapper.xml
<mappers>
<mapper resource="com/wgg/dao/UserMapper.xml"/>
</mappers>
- 测试类
public class TestDemo01 {
@Test
public void selectUser(){
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> users = mapper.selectUser();
for (User user : users) {
System.out.println(user);
}
session.close();
}
}
运行结果
可能会产生maven静态资源过滤问题,pom.xml中添加
<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>
标签:xml,第一个,UserMapper,程序,session,user,mybatis,public,MyBatis01 来源: https://www.cnblogs.com/zhishu/p/13944471.html