1.File-New-Project
2.
3.一路next下去就好
4.在pom.xml文件中加入以下代码:
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.13version>
dependency>
1.下载mybatis源码包 https://github.com/mybatis/mybatis-3/releases
2.mybatis有两个配置文件,其在源码包中的位置如图所示
3.将其拿出放入项目中
4. Configuration.xml解析(xml文件名可以自定义)
<typeAliases>
<typeAlias alias="User" type="cn.wx.xx.utils.User"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value=""/>
transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/accounts"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="User.xml"/>
mappers>
5.User.xml文件解析(xml文件名可以自定义)
<mapper namespace="User">
<resultMap type="cn.wx.xx.utils.User" id="User">
<id column="UID" jdbcType="INTEGER" property="id"/>
<result column="accountname" jdbcType="VARCHAR" property="accountname"/>
<result column="password" jdbcType="VARCHAR" property="password"/>
<result column="email" jdbcType="VARCHAR" property="email"/>
<result column="status" jdbcType="VARCHAR" property="status"/>
<result column="code" jdbcType="VARCHAR" property="code"/>
resultMap>
<select id="findName" parameterType="int" resultType="User" >
select * from accounts where UID = #{id}
select>
1.User.java代码
package cn.wx.xx.utils;
public class User {
private int id;
private String accountname;
private String password;
private String email;
private String status;
private String code;
/*
*get and set 方法省略
*/
1.读取配置文件
Reader reader = Resources.getResourceAsReader("Configuration.xml");
2.使用SqlSessionFactory获取连接
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
3.打开一个连接会话
SqlSession sqlSession = sqlSessionFactory.openSession();
4.获取session
con = sqlSession.getSqlSession();
5.通过Sqlsession执行sql语句,并保存到User对象
User user = new User();
user = sqlSession.selectOne("User.findName",24);
/*两个参数,第一个是使用‘命名空间.id’调用xml中sql语句的配置
*第二个参数,是xml中,#{id}需要的值
*/
1.mysqlcon.java
package cn.wx.xx.dao;
import cn.wx.xx.utils.User;
import cn.wx.xx.utils.mysqlCon;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
public class testmysql {
public User cxinfo(){
mysqlCon con = new mysqlCon();
SqlSession sqlSession =null;
User user = new User();
try {
sqlSession = con.getSqlSession();
//通过Sqlsession执行sql语句
user = sqlSession.selectOne("User.findName",24);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(sqlSession != null){
sqlSession.close();
}
}
System.out.println(user.getAccountname());
return user;
}
public static void main(String[] args) {
testmysql t = new testmysql();
User user;
user = t.cxinfo();
System.out.println(user.getEmail());
}
}
2.testmysql.java
package cn.wx.xx.dao;
import cn.wx.xx.utils.User;
import cn.wx.xx.utils.mysqlCon;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
public class testmysql {
public User cxinfo(){
mysqlCon con = new mysqlCon();
SqlSession sqlSession =null;
User user = new User();
try {
sqlSession = con.getSqlSession();
//通过Sqlsession执行sql语句
user = sqlSession.selectOne("User.findName",24);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(sqlSession != null){
sqlSession.close();
}
}
System.out.println(user.getAccountname());
return user;
}
public static void main(String[] args) {
testmysql t = new testmysql();
User user;
user = t.cxinfo();
System.out.println(user.getEmail());
}
}
3.User.java
package cn.wx.xx.utils;
public class User {
private int id;
private String accountname;
private String password;
private String email;
private String status;
private String code;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getAccountname() { return accountname; }
public void setAccountname(String accountname) { this.accountname = accountname; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }
}