Mybatis学习(一)

时间:2022-03-07 20:11:43

1)先导入pom文件

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Mybatis</groupId>
<artifactId>com.mybatis</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>com.mybatis Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies> <!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- mysql-connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>com.mybatis</finalName>
</build>
</project>

2)创建配置文件

Mybatis学习(一)

3)数据库驱动文件:

Mybatis学习(一)

4)为mybatis配置mybatis.cfg.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>
<!-- 引入外部配置文件 -->
<properties resource="config.properties"></properties> <typeAliases>
<!-- 别名方式1,一个一个的配置 type中放置的是类的全路径,alias中放置的是类别名
<typeAliase type="com.cy.mybatis.beans.UserBean" alias="UserBean"/> -->
<!-- 别名方式2,自动扫描,将JAVA类的类名作为类的类别名 -->
<package name="com.mybatis.entity"/>
</typeAliases>
<!-- 配置mybatis运行环境 -->
<environments default="cybatis">
<environment id="cybatis">
<!-- type="JDBC" 代表使用JDBC的提交和回滚来管理事务 -->
<transactionManager type="JDBC" /> <!-- mybatis提供了3种数据源类型,分别是:POOLED,UNPOOLED,JNDI -->
<!-- POOLED 表示支持JDBC数据源连接池 -->
<!-- UNPOOLED 表示不支持数据源连接池 -->
<!-- JNDI 表示支持外部数据源连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments> <mappers>
<!-- 告知映射文件方式1,一个一个的配置
<mapper resource="com/cy/mybatis/mapper/UserMapper.xml" class="com.mybatis.mapper.UserMapper"/>-->
<!-- 告知映射文件方式2,自动扫描包内的Mapper接口与配置文件 -->
<package name="com/mybatis/mapper"/>
</mappers>
</configuration>

5)创建数据库表:

Mybatis学习(一)

6)编写实体类:

package com.mybatis.entity;

import java.io.Serializable;

public class User implements Serializable{

    private Integer id;
private String username;
private String password;
private Double account; public User() {
super();
} public User(Integer id, String username, String password, Double account) {
super();
this.id = id;
this.username = username;
this.password = password;
this.account = account;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public Double getAccount() {
return account;
} public void setAccount(Double account) {
this.account = account;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}

7)编写映射的数据访问接口:

package com.mybatis.mapper;

import com.mybatis.entity.User;

public interface UserMapper {

    public int saveUser(User user);

    public int updateUser(User user);

    public int deleteUser(User user);

    public int selectUserById(int id);

    public int selectAllUser();

}

8)配置数据访问接口映射xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.mapper.UserMapper">
<resultMap type="User" id="user">
<id property="id" column="id" javaType="java.lang.Integer"/>
<result property="username" column="username" javaType="java.lang.String"/>
<result property="password" column="password" javaType="java.lang.String"/>
<result property="account" column="account" javaType="java.lang.Double"/>
</resultMap> <insert id="saveUser" useGeneratedKeys="true" keyProperty="id">
insert into t_user(username,password,account) values(#{username},#{password},#{account})
</insert> </mapper>

9)因为没有与spring进行整合,所以认为提交事务,也并没有关闭session,(注意)

package com.mybatis.tool;

 import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class DBTools {
public static SqlSessionFactory sessionFactory; static{
try {
//使用MyBatis提供的Resources类加载mybatis的配置文件
Reader reader = Resources.getResourceAsReader("mybatis.cfg.xml");
//构建sqlSession的工厂
sessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
//创建能执行映射文件中sql的sqlSession
public static SqlSession getSession(){
return sessionFactory.openSession();
} }

10)测试:

package com.mybatis.service;

import org.apache.ibatis.session.SqlSession;

import com.mybatis.entity.User;
import com.mybatis.mapper.UserMapper;
import com.mybatis.tool.DBTools; public class UserService { public void save(User user){
SqlSession session = DBTools.getSession();
int count = session.getMapper(UserMapper.class).saveUser(user);
session.commit();
} public static void main(String[] args) {
User user= new User(null, "彬彬", "123456", 8.0);
new UserService().save(user);
} }

Mybatis学习(一)

成功,第一阶段完成