mybatis的入门指南

xiaoxiao2021-02-28  5

mybatis的入门指南

在mysql建表及插入数据

SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for `users` -- ---------------------------- DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `username` varchar(45) NOT NULL, `password` varchar(45) NOT NULL, `enabled` tinyint(4) NOT NULL DEFAULT '1', PRIMARY KEY (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of `users` -- ---------------------------- BEGIN; INSERT INTO `users` VALUES ('alex', '123456', '1'), ('mkyong', '123456', '1'), ('user', '654321', '1'); COMMIT; SET FOREIGN_KEY_CHECKS = 1;

在eclipse建立一个普通的maven java工程

选择maven工程,方便管理 选择普通的java工程 下一步,输入相关的名字,最终的项目结构如下: 名字不一样,结构是一样的。

添加项目的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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>mybatis01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mybatis01</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> </dependencies> </project>

使用mybatis查询数据

添加mybatis配置文件conf.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> <typeAliases> <typeAlias type="com.example.mybatis01.domain.User" alias="user" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true" /> <property name="username" value="root" /> <property name="password" value="123456@#" /> </dataSource> </environment> </environments> <mappers> <!-- 注册userMapper.xml文件, userMapper.xml位于com.example.mybatis01.mapping这个包下,所以resource写成com/example/mybatis01/mapping/userMapper.xml --> <mapper resource="com/example/mybatis01/mapping/userMapper.xml" /> </mappers> </configuration>

typeAlias节点给实体类取一个别名,方便后面引用,environment节点是数据库连接的相关信息,mapper节点指明映射文件的地址,上面的文件内容如下: User.java

package com.example.mybatis01.domain; public class User { private String username; private String password; private boolean enabled; 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 boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } @Override public String toString() { return "User [username=" + username + ", password=" + password + ", enabled=" + enabled + "]"; } }

UserMapper.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.example.mybatis01.mapping.UserMapper"> <insert id="insert" parameterType="user"> insert into users(username, password,enabled) values(#{username}, #{password},#{enabled}) </insert> <select id="selectUser" parameterType="String" resultType="user"> SELECT * FROM users WHERE username = #{username} </select> <select id="selectAll" resultType="user"> SELECT * FROM users </select> </mapper>

然后写个App.java main类,测试使用mybatis访问到的数据,

package com.example.mybatis01; import java.io.InputStream; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.example.mybatis01.domain.User; /** * Hello world! * */ public class App { public static void main(String[] args) { //mybatis的配置文件 String resource = "conf.xml"; //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件) InputStream is = App.class.getClassLoader().getResourceAsStream(resource); //构建sqlSession的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); //创建能执行映射文件中sql的sqlSession SqlSession session = sessionFactory.openSession(); /** * 映射sql的标识字符串, * com.example.mybatis01.mapping.UserMapper是userMapper.xml文件中mapper标签的namespace属性的值, * selectUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL */ String statement = "com.example.mybatis01.mapping.UserMapper.selectUser";//映射sql的标识字符串 //执行查询返回一个唯一user对象的sql User user = session.selectOne(statement, "alex"); System.out.println(user); System.out.println(session.selectList("com.example.mybatis01.mapping.UserMapper.selectAll")); } }

最后项目的结构如下: 运行main方法,打印出结果如下:

转载请注明原文地址: https://www.6miu.com/read-1600022.html

最新回复(0)