【自我介绍】------Mr.张小白(案例:员工管理系统的MyBatis的实现)

本文详细介绍了如何从零开始搭建MyBatis开发环境,包括创建工程、引入依赖、配置数据库连接、创建核心配置文件、编写POJO实体、映射文件及测试类。通过实例展示了MyBatis的基本操作,如查询、增删改。适合初学者跟随实践。
摘要由CSDN通过智能技术生成

@自我介绍

欢迎各位来看我的这篇文章

你好! 这是我第一次使用发表文章。操作的还不太熟练,在接下来的日子里,希望我能够和CSDN这个大家庭一起成长,一起进步。

新的改变(IDEA使用)

我现在是一名在校大学生,正在跟随老师做一些代码项目,希望借助CSDN这个平台,能够记录下来我的成长点滴,跟大家一起进步。
下面是我第一天做的案例,请各位前辈多多指教,如有不足之处,私聊我,看到信息会立马改正!!!

OneDay----MyBatis

MyBatis环境搭建

1.创建工程

  1. 第一步
    第一步
  2. 第二步
    第二步
  3. 第三步
    第三步
  4. 第四步
    第四步

2.引入相关依赖

<?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.zjd</groupId>
    <artifactId>Part2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

3.创建数据库

//使用MySql管理工具Nactive创建mybatis数据库
create database mybatis

4.创建数据库连接信息配置文件

//创建db.properties文件
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
mysql.username=root
mysql.password=root

5.创建MyBatis的核心配置文件

<?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="db.properties"/>
    <settings>
        <setting name="cacheEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.zjd.pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${mysql.driver}"/>
                <property name="url" value="${mysql.url}"/>
                <property name="username" value="${mysql.username}"/>
                <property name="password" value="${mysql.password}"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

6.MyBatis入门程序

1.数据准备
//使用MySql管理工具Nactive创建表employee并插入数据
use mybatis;
create table employee(
    id int primary key auto_increment,
    name varchar(20) not null,
    age int not null,
    position varchar(20)
);
insert into employee(id,name,age,position) values(null,'张三',20,'员工  	'),(null,'李四',18, '员工'),(null,'王五',35,'经理');

2.创建POJO实体
package com.zjd.pojo;

public class Employee {
    private int id;
    private String name;
    private int age;
    private String position;

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", position='" + position + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }
}

3.创建映射文件
<?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">
<mapper namespace="com.zjd.pojo.Employee">
    <select id="findById" parameterType="int" resultType="Employee">
        select * from employee where id=#{id}
    </select>
    <insert id="addOne" parameterType="Employee" keyProperty="id" useGeneratedKeys="true">
        insert into employee(name,age,position)values (#{name},#{age},#{position})
    </insert>
    <update id="updateOne" parameterType="Employee">
        update employee set name=#{name},age=#{age},position=#{position} where id=#{id}
    </update>
    <delete id="deleteOne" parameterType="int">
        delete from employee where id=#{id}
    </delete>
</mapper>
4.修改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">
<configuration>
    <properties resource="db.properties"/>
    <settings>
        <setting name="cacheEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.zjd.pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${mysql.driver}"/>
                <property name="url" value="${mysql.url}"/>
                <property name="username" value="${mysql.username}"/>
                <property name="password" value="${mysql.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/zjd/mapper/EmployeeMapper.xml"/>
    </mappers>
</configuration>
5.编写工具类
package com.zjd.utils;

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.io.Reader;

public class MyBatisUtils {
    public static SqlSessionFactory sqlSessionFactory=null;
    static {
        try {
            Reader reader= Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static  SqlSession getSession(){
        return sqlSessionFactory.openSession();
    }
}

6.编写测试类
package Test;

import com.zjd.pojo.Employee;
import com.zjd.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class EmployeeTest {
    SqlSession session=null;
    @Before
    public void Before(){
        session=MyBatisUtils.getSession();
    }
    @Test
    public void findById(){
        List<Employee> employees=session.selectList("com.zjd.pojo.Employee.findById",1);
        for (Employee employ:employees
             ) {
            System.out.println(employ);
        }
    }

    @Test
    public void addOne(){
        Employee employee=new Employee();
        employee.setName("zjd");
        employee.setAge(18);
        employee.setPosition("Boss");
        int result=session.insert("com.zjd.pojo.Employee.addOne",employee);
        if (result>0){
            System.out.println("succeed");
        }else {
            System.out.println("failed");
        }
    }

    @Test
    public void updateOne(){
        Employee employee=new Employee();
        employee.setId(1);
        employee.setName("zjd");
        employee.setAge(18);
        employee.setPosition("Boss");
        int result=session.update("com.zjd.pojo.Employee.updateOne",employee);
        if (result>0){
            System.out.println("succeed");
        }else {
            System.out.println("failed");
        }
    }

    @Test
    public void deleteOne(){
        Employee employee=new Employee();
        int result=session.delete("com.zjd.pojo.Employee.deleteOne",1);
        if (result>0){
            System.out.println("succeed");
        }else {
            System.out.println("failed");
        }
    }
    @After
    public void After(){
        session.commit();
        session.close();
    }
}

7.项目结构

项目结构

项目:员工管理系统

实现功能

1.根据id查询员工信息
2.新增员工信息
3.根据id修改员工信息
4.根据id删除员工信息

总结

以上就是我本次项目的过程步骤,如果有兴趣的小伙伴可以看看,如有不懂之处,欢迎咨询
如有不足之处,欢迎各位前辈前来批评指正

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值