Myabits配置

生产环境: eclipse + navicat + mysql

Snipaste_2020-10-10_11-22-20.png

pom.xml设置

  • 这里需要注意的点是mysql-connector-java的版本号和mysql本身的版本要保持一致(也可以是在同一个大版本号下如mysql版本为4.5.1,mysql-connector-java版本就为4.5.X)

  4.0.0

  com.example
  department
  0.0.1-SNAPSHOT
  jar

  department
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      3.8.1
      test
    
    
        org.mybatis
        mybatis
        3.4.6
    
    
          mysql
          mysql-connector-java
          8.0.15
    
    
  


myabits-config.xml配置

  • 这里需要注意的问题是driver的版本,低版本的不需要cj,高版本的需要cj
//低版本
 
 //高版本(如如如8.0.00),高版本需要加时区
 
 
  • mysql数据库用户名和密码





  
    
      
      
        
        
        
        
      
    
  
  
    
  

xxxMapper.xml(我理解为xml的数据库语句)

需要注意的点:

  • mapper的命名空间尽量和Mapper所在的架包下
  • parameterType:参数类型,可以是java普通数据类型,也可以是自定义的实体类
  • resultType:返回值,如果bean与数据库一一对应,则会自动映射







     insert into department (id,info,leader,u) values (#{id},#{info},#{leader},#{u});


update department set info =#{info},leader=#{leader},u=#{u} where id = #{id}


delete from department where id = #{id}


java代码

package com.example.department;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.annotations.Param;
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 com.example.department.Bean.Department;
import com.example.department.Bean.Student;

public class UserTest {
    //resource为文件路径
    private static String resource="com/example/department/myabits-config.xml";
    //xml为java . 路径
    private static String xml = "com.example.department.UserMapper.";
    //select,selet方法可以不用commit
    public static void selectDepartment(@Param("department")Department department) {
        try {
            InputStream inputStream = Resources.getResourceAsStream(UserTest.resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            //搜索一个数据库对象
            Department user = session.selectOne(UserTest.xml+"selectDepartment",department);
            System.out.println(user);
            session.close();
            inputStream.close();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    //update
    public static void updateDepartment(Department department) {
        try {
            InputStream inputStream = Resources.getResourceAsStream(UserTest.resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            session.update(UserTest.xml+"updateDepartment",department);
            session.commit();
            session.close();
            inputStream.close();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    //delete
    public static void deleteDepartment(Department department) {
        try {
 
            InputStream inputStream = Resources.getResourceAsStream(UserTest.resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            session.delete(UserTest.xml+"deleteDepartment",department);
            session.commit();
            session.close();
            inputStream.close();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    //add
    public static void addDepartment(@Param("department")Department department) {
        try {
            InputStream inputStream = Resources.getResourceAsStream(UserTest.resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            session.insert(UserTest.xml+"addDepartment",department);
            session.commit();
            session.close();
            inputStream.close();
        }catch(IOException e) {
            e.printStackTrace();
        
    }
    
    
    }
}

记一个究极傻逼的错误,我在用写sql语句作为了key,花了我一晚上来解决,命名不规范,两行泪呀!

你可能感兴趣的:(Myabits配置)