MyBatis项目的创建步骤及存在问题

MyBatis项目的创建步骤及存在问题

1.创建Maven项目

添加依赖mybatis
添加依赖mysql驱动
准备数据库

(1)添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<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.dyy</groupId>
<artifactId>mybatis-class</artifactId>
<version>1.0.0</version>
<name>mybatis-class</name>
<description>mybatis框架学习</description>

<dependencies>
    <!--mybatis框架依赖-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.5</version>
    </dependency>

    <!--数据库驱动依赖-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>
</dependencies>
</project>

(2)准备数据库

2.创建mybatis配置

 src/main/resources->mybatis-config.xml
 配置文件中需要配置数据库信息,driver, url, username, password

(1)编写mybatis-config.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">

<!--2.配置文件中需要配置的数据库信息-->
<configuration>
<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC"></transactionManager>
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/memo"/>
            <property name="username" value="root"/>
            <property name="password" value="******"/>
        </dataSource>
    </environment>
</environments>

<mappers>
    <mapper resource="mapper/mapper.xml"/>
</mappers>

(2)配置文件中需要的数据库信息

<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/memo"/>
<property name="username" value="root"/>
<property name="password" value="******"/>

3.编码

(1)准备entity包 实体类->数据库

package com.dyy.mybatis.entity;
import java.util.Date;
//实体类-》数据库
public class MemoGroup {
   private Integer id;
   private String name;
   private Date creatTime;
   private Date modifTime;
  public Integer getId() {
   return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Date getCreatTime() {
    return creatTime;
}
public void setCreatTime(Date creatTime) {
    this.creatTime = creatTime;
}
public Date getModifTime() {
    return modifTime;
}
public void setModifTime(Date modifTime) {
    this.modifTime = modifTime;
}
@Override
public String toString() {
    return "MemoGroup{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", creatTime=" + creatTime +
            ", modifTime=" + modifTime +
            '}';
            }
      }

(2)mapper包 mapper包->数据库操作方法

package com.dyy.mybatis.mapper;
import com.dyy.mybatis.entity.MemoGroup;
//数据库操作方法
public interface MemoGroupMapper {
    int insertMemoGroup(MemoGroup memoGroup);
    //queryMemoGroup();
}

(3)src/main/resources/mapper 创建mapper.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" >
<!--namespace应该为全限定名-->
<mapper namespace="com.dyy.mybatis.mapper.MemoGroupMapper">
<insert id="insertMemoGroup">
    insert into memo_group(name,created_time) values (#{name},#{creatTime})
</insert>
</mapper>

(4)编码,创建SqlSessionFactory SqlSession Mapper Interface

package com.dyy.mybatis;

import com.dyy.mybatis.entity.MemoGroup;
import com.dyy.mybatis.mapper.MemoGroupMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.Date;

public class MyBatisApplication {

public static SqlSessionFactory sessionFactory;
public static void buildSqlSessionFactoryWithXml(){
    try {
        sessionFactory  = new SqlSessionFactoryBuilder().build
                (Resources.getResourceAsStream("mybatis-config.xml"));
        System.out.println(sessionFactory);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    buildSqlSessionFactoryWithXml();
    SqlSession sqlSession = sessionFactory.openSession(true);
    MemoGroup memoGroup = new MemoGroup();
    memoGroup.setName("dyy");
    memoGroup.setCreatTime(new Date());
    MemoGroupMapper memoGroupMapper = sqlSession.getMapper(MemoGroupMapper.class);
    int effect = memoGroupMapper.insertMemoGroup(memoGroup);
    System.out.println(effect);
    sqlSession.close();
    }
}

问题:

容易拼写错误
文件夹,包名,类名取名问题
mybatis-config.xml环境的ID名称,简明
mapper文件中的namespace是mapper接口的全限定名
写mapper中的命令insert select delete update 通过mapper接口的方法名直接获取

猜你喜欢

转载自blog.csdn.net/qq_40409115/article/details/80919378
今日推荐