Mybatis学习(一)

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>
View Code

2)创建配置文件

3)数据库驱动文件:

 

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)创建数据库表:

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;
    }
}
View Code

 

 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();
    
}
View Code

 

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();
    }
     
}
View Code

 

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);
    }
    
}

 

 

 

 成功,第一阶段完成

 

转载于:https://www.cnblogs.com/yubin-jia/p/6233380.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值