栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

初始Mybatis做java项目

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

初始Mybatis做java项目

1、创建一个Maven JAVA工程exp

2、在mysql数据库中创建表;

DROp TABLE IF EXISTS exp_stu;
CREATE TABLE exp_stu(
    stuno int,
    name  varchar(20),
    sex   varchar(20),
    score int
);

3、在pom.xml中配置


	
	    org.mybatis
	    mybatis
	    3.4.6
	
	
	
	    mysql
	    mysql-connector-java
	    5.1.48
	

4、配置mybatis-config.xml

特别注意:

      这里的stu_info是你要连接的数据库的表名

   value  这里填自己的数据库名
    value  这里填自己的数据库密码

   
   
    
   
       
           
           
           
           
       
    
          
           
              
              
                 
              
              
           
       
     
    
        
       
  

 

5、实现MyBatisUtil.java

package demo.exp4_mybatis;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {
	private final static SqlSessionFactory sqlSessionFactory;
	static {
		String resource = "mybatis-config.xml";
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(resource);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
	}

	public static SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}
}

6、写接口StudentMapper.java,用注解方式完成向exp_stu表的插入操作和查询操作

ps:StudentMapper是一个接口,它的实现就是上面的注解形式,即SQL语句

package demo.exp4_mybatis;

import java.util.Map;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public interface StudentMapper {

//用注解方式完成向exp_stu表的插入操作和查询操作;
	@Insert("inert into exp_stu values(#{stuno},#{name},#{sex},#{score})")
	public void insertStu(@Param("stuno")Integer stuno,@Param("name")String name,@Param("sex")String sex,@Param("score")Integer score);
	
	@Select("select*from exp_stu where stuno=#{stuno}")
	public Map getStu(int stuno);
}

7、在主程序中实现向数据库中插入一条记录(1001, zhangshan, male, 95),并查询1001,在屏幕上打印输出姓名和成绩

package demo.exp4_mybatis;

import java.util.Map;

import org.apache.ibatis.session.SqlSession;


public class Test 
{
	public static void insertStu(Integer stuno, String name, String sex, Integer score) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
		
		try {
			StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
			studentMapper.insertStu(stuno, name, sex, score);
			sqlSession.commit();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			sqlSession.close();
		}
	}
	
	public static void getStu(int stuno) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
		
		try {
			StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
			Map student =studentMapper.getStu(stuno);
			System.out.println("name:"+student.get("name"));
			System.out.println("sex:"+student.get("sex"));
			System.out.println("score:"+(Object)(student.get("score")));
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		sqlSession.close();
		
	}
	
    public static void main( String[] args )
    {
    	
//    	在主程序中实现向数据库中插入一条记录(1001, zhangshan, male, 95),
//    	并查询1001,在屏幕上打印输出姓名和成绩
    	insertStu(1001,"zhangsan","male",95);
    	getStu(1001);
    	
        
    }
}

常见问题:

1、

mybatis-config.xml放置的位置,和java程序在同一包下。

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/921718.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号