使用IDEA、Mybatis配置的sql数据库的增删改查

pom.xml:



<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.0modelVersion>

  <groupId>textgroupId>
  <artifactId>MybatisartifactId>
  <version>1.0.0version>
  <packaging>jarpackaging>

  <dependencies>
    
    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>3.4.6version>
    dependency>

    
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>5.1.38version>
    dependency>

    
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
    dependency>

    
    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <version>1.16.20version>
      <scope>providedscope>
    dependency>

  dependencies>

project>

Mybatis.xml:



<configuration>
    <environments default="development">
        <environment id="development">
            
            <transactionManager type="JDBC"/>
            
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql:///mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="123"/>
            dataSource>
        environment>
    environments>
    
    <mappers>
        
        <mapper resource="text/Mybatis/dao/UserMapper.xml">mapper>
    mappers>
configuration>

UserMapper.xml:





<mapper namespace="text.Mybatis.dao.IUserDao">

    <insert id="save">
        insert into user(name,age,birthday,salary)
        VALUES (#{name},#{age},#{birthday},#{salary})
    insert>

    <update id="update">
        update user set
        name=#{name},
        age=#{age},
        birthday=#{birthday},
        salary=#{salary}
        WHERE
        id=#{id}
    update>

    <delete id="delete">
        delete from user where id=#{id}
    delete>

    <select id="get" resultType="text.Mybatis.bean.User">
        select * from user where id=#{id}
    select>
    
    <select id="list" resultType="text.Mybatis.bean.User">
        select * from user
    select>

mapper>

App.java:

import text.Mybatis.bean.User;
import text.Mybatis.dao.IUserDao;
import text.Mybatis.dao.impl.UserDaoImpl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.apache.ibatis.io.Resources;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by Administrator on 2019/8/28.
 */

public class App {
    @Test
    public void testSqlSessionFactory() throws Exception {

        //获取SQLSessionFactory对象
        String resource="Mybatis.xml";
        InputStream in = Resources.getResourceAsStream(resource);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = sqlSessionFactory.openSession();

        System.out.println(sqlSessionFactory);

    }

    @Test
    public void testSave() throws Exception {
        IUserDao dao=new UserDaoImpl();
        User user=new User(null,"jack",20,new Date(1000),new BigDecimal(3000.00));
        dao.save(user);
    }

    @Test
    public void testUpdate() throws Exception {
        IUserDao dao=new UserDaoImpl();
        User user=new User(2,"tom",30,new Date(),new BigDecimal(5000.00));
        dao.update(user);
    }
    @Test
    public void testDelete() throws Exception {
        IUserDao dao=new UserDaoImpl();
        dao.delete(2);

    }
    @Test
    public void testGet() throws Exception {
        IUserDao dao=new UserDaoImpl();
        System.out.println(dao.get(1));

    }
    @Test
    public void testList() throws Exception {
        IUserDao dao=new UserDaoImpl();
        System.out.println(dao.list());

    }
}

User.java:

package text.Mybatis.bean;

import lombok.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by Administrator on 2019/8/28.
 */
@Setter@Getter@ToString
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    private String name;
    private Integer age;
    private Date birthday;
    private BigDecimal salary;
}

MybatisUtil.java:

package text.Mybatis.util;

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.InputStream;

/**
 * Created by Administrator on 2019/8/28.
 */
public class MybatisUtil {
    public static SqlSessionFactory factory=null;
    static {
        String resource="Mybatis.xml";
        InputStream in = null;
        try {
            in = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }

        factory = new SqlSessionFactoryBuilder().build(in);
    }

    public static SqlSession getsession (){

        return factory.openSession();
    }


}

IUserDao.java(接口):

package text.Mybatis.dao;

import text.Mybatis.bean.User;

import java.util.List;

/**
 * Created by Administrator on 2019/8/28.
 */
public interface IUserDao {
    void save(User user);
    void update(User user);
    void delete(Integer id);

    User get(Integer id);
    List<User> list();
}

UserDaoImpl.java:

package text.Mybatis.dao.impl;

import text.Mybatis.bean.User;
import text.Mybatis.dao.IUserDao;
import text.Mybatis.util.MybatisUtil;
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.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2019/8/28.
 */
public class UserDaoImpl implements IUserDao{

    public void save(User user) {
        //执行保存操作
//        获取SqlSession对象
        SqlSession session = MybatisUtil.getsession();
//        执行保存的sql语句
//        namespace的值加上id值
        session.insert("text.Mybatis.dao.IUserDao.save",user);
//        提交事务
        session.commit();
//        关闭session
        session.close();
    }
//  更新操作
    public void update(User user) {
        SqlSession session = MybatisUtil.getsession();
        session.update("text.Mybatis.dao.IUserDao.update",user);
        session.commit();
        session.close();

    }

    public void delete(Integer id) {
        SqlSession session = MybatisUtil.getsession();
        session.delete("text.Mybatis.dao.IUserDao.delete",id);
        session.commit();
        session.close();
    }

    public User get(Integer id) {
        SqlSession session = MybatisUtil.getsession();
//        查询一个
        User user = (User) session.selectOne("text.Mybatis.dao.IUserDao.get", id);

        session.commit();
        session.close();

        return user;

    }

    public List<User> list() {
        SqlSession session = MybatisUtil.getsession();
//        查询列表
        List<User> list = session.selectList("text.Mybatis.dao.IUserDao.list");

        session.commit();
        session.close();

        return list;


    }
}

你可能感兴趣的:(IDEA,Mybatis,数据库)