1.1.1. 第一步:导入包

 

 

 

1.1.2. 第二步:创建一个总配置文件mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" >

<!-- XMLConfigBuilder是总配置文件的构建类 -->

<configuration>

  <!-- 用于配置数据库的连接参数 ,environments这个标签可以配置多个数据库的连接,default表示默认使用的配置-->

  <environments default="default-msql">

  

     <!-- 数据库的连接参数 Configuration查看参数-->

     <!-- 所有框架的静态属性都可以在这个框架的类里面找到,不用死记硬背 -->

     <!-- id:就是一个标识符 -->

     <environment id="default-msql">

        <!-- 事务类型 -->

        <!-- 事务类型有哪些

         JDBC:使用JDBC事务

         MANAGED:不用事务

         -->

        <transactionManager type="JDBC"></transactionManager>

        <!-- 数据源 -->

        <!-- type表示数据库的类型

           JNDI:使用JNDI获得数据源.

           POOLED:使用默认的连接池获得数据源

           UNPOOLED:不用数据源

         

          -->

          <!-- org.apache.ibatis.datasource.pooled.PooledDataSource -->

        <dataSource type="POOLED">

           <!-- 所有的property对应的是一个set方法 -->

           <!-- 四要素 -->

           <property name="driver" value="org.gjt.mm.mysql.Driver"/>

           <property name="url" value="jdbc:mysql://localhost:3306/school"/>

           <property name="username" value="root"/>

           <property name="password" value="123456"/>

        </dataSource>

     </environment>

  </environments>

  <!-- 加载映射文件 -->

  <mappers>

    <!-- class属性:加载的是使用注解调用方式的接口 -->

    <mapper class="cn.gzsxt.mapper.StudentMapper"/>

  </mappers>

 

</configuration>

 

 

1.1.3. 第三步:创建一个DbUtils,获得SqlSession操作对象

package cn.lxm.utils;

 

import java.io.IOException;

import java.io.InputStream;

 

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

 

/**

 * 这个一个数据库连接帮助类,用于获得操作数据库的SqlSession对象

 * @author Administrator

 *

 */

public class DbUtils {

 

//1.获得SqlSessionFactory

 

public static SqlSessionFactory createSqlSessionFactory(){

//1.通过Resources读取配置文件

try {

//获得一个总配置文件的输入流

InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

//2.创建一个SqlSessionFactoryBuilder

SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();

//获得会话工厂

SqlSessionFactory sqlSessionFactory = builder.build(inputStream);

return sqlSessionFactory;

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

return null;

}

 

//2.获得SqlSession

public static SqlSession getSession(){

 

SqlSessionFactory sqlSessionFactory = DbUtils.createSqlSessionFactory();

return sqlSessionFactory.openSession();

}

 

public static void main(String[] args) {

System.out.println(DbUtils.getSession());

}

 

 

}

 

 

 

1.1.4. 第四步:创建一个映射接口+操作的注解

package cn.lxm.mapper;

 

import java.util.List;

 

import org.apache.ibatis.annotations.Delete;

import org.apache.ibatis.annotations.Insert;

import org.apache.ibatis.annotations.Select;

import org.apache.ibatis.annotations.Update;

 

import cn.lxm.entity.Student;

 

public interface StudentMapper {

 

/**

 * 插入记录

 * @param entity

 * @return

 */

@Insert(value="INSERT INTO student(AID, SNAME, SEX, BIRTHDAY, AGE) VALUES (#{sid}, #{sname}, #{sex}, #{birthday}, #{age})")

int insert(Student entity);

 

/**

 * 删除记录

 * @param sid

 * @return

 */

@Delete(value="DELETE FROM student WHERE SID=#{sid}")

int deleteById(int sid);

 

/**

 * 更新记录

 * @param entity

 * @return

 */

@Update(value="UPDATE student SET AID=#{aid},SNAME=#{sname},SEX=#{sex},BIRTHDAY=#{birthday},AGE=#{age} WHERE SID=#{sid}")

int update(Student entity);

/**

 * 查询所有的记录

 * @return

 */

@Select(value="SELECT * FROM student")

List<Student> findAll();

 

}

 

 

1.创建一个实体类

package cn.lxm.entity;

 

import java.util.Date;

 

//ctrl+shift+y 大写转小写

public class Student {

 

private Integer sid;//int(11) not null auto_increment comment '编号',

private Integer aid;//int(11) null default '0',

private String sname;//varchar(50) null default null comment '姓名',

private String sex;//char(3) null default null comment '性别',

private Date birthday;//date null default null comment '生日',

private Integer age;//int(11) null default null comment '年龄',

public Integer getSid() {

return sid;

}

public void setSid(Integer sid) {

this.sid = sid;

}

public Integer getAid() {

return aid;

}

public void setAid(Integer aid) {

this.aid = aid;

}

public String getSname() {

return sname;

}

public void setSname(String sname) {

System.out.println("-输出名字-"+sname);

this.sname = sname;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public Date getBirthday() {

return birthday;

}

public void setBirthday(Date birthday) {

this.birthday = birthday;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

 

}

 

 

1.1.5. 第五步:测试代码

package cn.lxm.test;

 

import static org.junit.Assert.*;

 

import java.util.Date;

import java.util.List;

 

import org.apache.ibatis.session.SqlSession;

import org.junit.Test;

 

import cn.lxm.entity.Student;

import cn.lxm.mapper.StudentMapper;

import cn.lxm.utils.DbUtils;

 

public class StudentMapperTest {

 

/* @Test

public void insert() {

try {

//1.获得操作对象

SqlSession session = DbUtils.getSession();

//2.使用session.获得映射接口的代理对象,这个代理对象是mybatis通过StudentMapper.xml创建的

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

Student student = new Student();

student.setSname("王五");

student.setSex("男");

//调用接口操作数据库

studentMapper.insert(student);

//提交

session.commit();

session.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

}*/

 

/* @Test

public void deleteById(){

//1.获得操作对象

SqlSession session = DbUtils.getSession();

//2.使用session.获得映射接口的代理对象,这个代理对象是mybatis通过StudentMapper.xml创建的

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

int count=studentMapper.deleteById(1);

System.out.println(count);

session.commit();

session.close();

}*/

 

 

/* @Test

public void update(){

//1.获得操作对象

SqlSession session = DbUtils.getSession();

//2.使用session.获得映射接口的代理对象,这个代理对象是mybatis通过StudentMapper.xml创建的

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

Student entity=new Student();

entity.setSname("赵六");

entity.setSex("男");

entity.setAge(20);

entity.setBirthday(new Date());

entity.setSid(2);

 

//更新记录

int count=studentMapper.update(entity);

System.out.println(count);

session.commit();

session.close();

}*/

 

@Test

public void findAll(){

//1.获得操作对象

SqlSession session = DbUtils.getSession();

//2.使用session.获得映射接口的代理对象,这个代理对象是mybatis通过StudentMapper.xml创建的

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

List<Student> list = studentMapper.findAll();

for(Student s:list){

System.out.print("姓名:"+s.getSname());

System.out.print(",年龄:"+s.getAge());

System.out.println(",生日:"+s.getBirthday());

}

 

session.commit();

session.close();

}

}