Java教程

在Idea中创建maven工程,搭建mybatis框架并完成数据库表的增删改查操作

本文主要是介绍在Idea中创建maven工程,搭建mybatis框架并完成数据库表的增删改查操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.创建maven工程

(1)首先打开idea点击idea中的File->New->Project...

(2)在左侧找到Maven如图所示:

 

然后点击Next

在配置好工程名后点击Next

 

 然后确认工程路径点击Finish创建。

 (3)创建一个数据库和表结构如下:

库名student,表名student

 在添加几条记录:

 

2.搭建mybatis框架 

在src文件夹中创建lib文件夹导入mysql与mybatis的jar包并将包添加到库中。

在创建包与类结构如下: 

 (1)在Users类中编写如下:

package com.soutwind.entity;

public class Users {
    private int id;
    private String username;
    private String password;
    public Users(){

    }
    public Users(String username,String password){
        this.username=username;
        this.password=password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}

 2)在pom.xml中编写如下代码配置框架信息:

<?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>ma_my</groupId>
    <artifactId>ma_my工程</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connection-java</artifactId>
            <version>8.0.11</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

3)在resources中创建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">
<configuration>
    <!-- 配置MyBatis运行环境-->
    <environments default="development">
        <environment id="development">
            <!--                配置jdbc的事务管理-->
            <transactionManager type="JDBC">
            </transactionManager>
            <!--                pooled配置jdbc数据源连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/190506?useUnicode=true&amp;characterEncoding=GBK"></property>
                <property name="username" value="root"></property>
                <property name="password" value="x5"></property>

            </dataSource>

        </environment>
    </environments>

    <!--        注册UsersMapper.xml-->
    <mappers>
        <mapper resource="mapper/UsersMapper.xml"></mapper>
    </mappers>
</configuration>

3.创建测试类Test并对单表进行增删改查,在其中编写如下代码:

package com.soutwind.test;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class Test {
    public static  void main(String[] args){
        //加载MyBatis配置文件
        InputStream in=Test.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactoryBuilder der=new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory= der.build(in);
        SqlSession sqlSession=sqlSessionFactory.openSession();

        String insert="resources.mapper.UsersMapper.insert";
        String select="resources.mapper.UsersMapper.select";
        String delete="resources.mapper.UsersMapper.delete";
        String update="resources.mapper.UsersMapper.update";
        //添加记录
        /*Users user=new Users("张三","321");
        sqlSession.insert(insert,user);*/

        //删除记录
        /*Users user=new Users();
        user.setId(7);
        sqlSession.delete(delete,user);*/

        //修改记录
        /*Users user=new Users("李四","123");
        user.setId(5);
        sqlSession.update(update,user);*/

        //查询记录
        /*List<Users> list=sqlSession.selectList(select);
        for(int i=0;i<list.size();i++){
            Users user=list.get(i);
            System.out.println("id:"+user.getId());
        }*/
        // 提交事务 增删改 需要commit,查询无需commit
        sqlSession.commit();
        //关闭会话
        sqlSession.close();
    }

}

这篇关于在Idea中创建maven工程,搭建mybatis框架并完成数据库表的增删改查操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!