利用mybatis连接mysql数据库_mybatis数据库访问使用案例

使用mybatis首先要导入两个包到lib文件下mybatis-3.2.7.jar,ojdbc6.jar,本次用的数据库是Oracle,一个为mybatis使用所用包,一个为jdbc连接数据库所用的包。

在src文件下面我们需要定义mybatic的配置文件mybatis-config.xml

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

本文出自:http://www.solgle.com/news/358.html

下面new了一个包usebatis,创建了几个java类文件。MyBatisUtil 主要用于获得SqlSessionFactory

package usebatis;

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 {

Reader reader = null;

try {

reader = Resources.getResourceAsReader("mybatis-config.xml");

} catch (Exception e) {

e.printStackTrace();

}

sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

}

public static SqlSessionFactory getSqlSessionFactory() {

return sqlSessionFactory;

}

}

package usebatis;

import beans.Student;

创建一个接口java文件StudentMapper,以及配置文件StudentMapper.xml

public interface StudentMapper {

public void insertStudent(Student student);

public Student getStudent(String sno);

}

/p>

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

insert into student(sno,sname,ssex,sbirthday,sclass) values (seq_mysource.nextval,#{sname},#{ssex},#{sbirthday},#{sclass})

select * from student where sno=#{sno}

最后是数据操作调用

package usebatis;

import java.text.SimpleDateFormat;

import java.util.Date;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import beans.Student;

public class ADOStudent {

static SqlSessionFactory sqlSessionFactory = null;

static {

sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();

}

public static void studentAdd() {

System.out.println("load xml beginng");

SqlSession sqlSession = sqlSessionFactory.openSession();//打开数据库会话

System.out.println("load xml end");

try {

//由会话实例化mapper接口

StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);

Student student = new Student();

//sno由数据库序列生成

//student.setSno("301");

student.setSname("10101");

student.setSsex("女");

student.setSclass("20220");

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date date = (Date) sdf.parse("1990-08-08");

student.setSbirthday(date);

studentMapper.insertStudent(student);

sqlSession.commit();// 这里一定要提交,不然数据进不去数据库中

System.out.println("get sno:"+student.getSno());//数据库sno序列号返回

System.out.println("insert sucess");

}catch(Exception e){

sqlSession.rollback();

e.printStackTrace();

} finally {

sqlSession.close();

}

}

public static void getStudent() {

SqlSession sqlSession = sqlSessionFactory.openSession();

try {

StudentMapper userMapper = sqlSession.getMapper(StudentMapper.class);

Student student = userMapper.getStudent("301");

System.out.println("sno: " + student.getSno() + "|sname: "

+ student.getSname());

}catch(Exception e){

e.printStackTrace();

}

finally {

sqlSession.close();

}

}

public static void main(String args[]) {

studentAdd();

getStudent();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值