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;
选择maven工程,方便管理
选择普通的java工程
下一步,输入相关的名字,最终的项目结构如下:
名字不一样,结构是一样的。
<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.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>mybatis01artifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>mybatis01name>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.2.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>6.0.6version>
dependency>
dependencies>
project>
<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>
<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
<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"));
}
}