目录
前言
本文供以下文章参考使用:
提示:
本文顺序在文章:Spring基础回顾__evenif的博客-CSDN博客之后。其中的项目代码,在该文基础上进行了扩充,如果根据本文代码无法正确构建自己的测试环境,请先学习spring相关知识后再来。
1 Index.java
package com.evenif.bean;
public class Index {
private int id;
private String name;
public Index() {
// System.out.println("create Index class");
}
public Index(int id, String name) {
this.id = id;
this.name = name;
// System.out.println("create Index class with params");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Index{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
2 IndexDao.java
package com.evenif.dao;
import com.evenif.bean.Index;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface IndexDao {
@Select("select * from tbl_index where id=#{id}")
Index getById(int id);
@Select("insert into tbl_index(id,`name`)values(#{id},#{name})")
void insert(Index index);
//如果参数是bean类,则会自动映射,如下所示,#{name} 必须是类中的同名属性
@Update("update tbl_index set `name`=#{name} where id=#{id}")
void update(Index index);
// 使用注解时比较像mybatis-spring的形式,如果是一个参数,自动识别,写#{id}或#{0}都能取得参数,其实和参数名称无关,例如以下定义int iddd也是可以的,当然一般都是一致的定义为
//int id 和#{id} 对应
@Delete("delete from tbl_index where id=#{id}")
void delete(int iddd);
//如果是超过一个参数,可以使用#{0},#{1} 或者使用如下的Param注解形式
@Select("select * from tbl_index limit #{start},#{size}")
List<Index> loadAll(@Param("start") int start, @Param("size") int size);
}
3 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 导入外部数据源配置文件-->
<properties resource="db.properties"/>
<typeAliases>
<!-- 别名方式一:给全类名指定别名,可以在mapper映射文件中简化引用-->
<!--
<typeAlias type="com.evenif.bean.Index" alias="Index"/>
-->
<!-- 别名方式二:给包下所有类名指定别名,默认别名是对应的类名-->
<package name="com.evenif.bean"/>
</typeAliases>
<!-- environments指mybatis可以配置多个环境 default指向默认的环境
每个SqlSessionFactory对应一个环境environment -->
<environments default="development">
<environment id="development">
<!-- JDBC - 这个配置直接使用JDBC的提交和回滚功能。它依赖于从
数据源获得连接来管理事务的生命周期。
MANAGER - 这个配置基本上什么都不做。它从不提交或者回滚一个连接
的事务。而是让容器(例如:Spring或者J2EE应用服务器)来管理事务
的生命周期-->
<transactionManager type="JDBC"/>
<!-- 数据源类型:
UNPOOLED - 这个类型的数据源实现只是在每次需要的时候简单地打
开和关闭连接。
POOLED - 这个数据源的实现缓存了JDBC连接对象,用于避免每次创
建新的数据库连接时都初始化和连接认证,加快程序响应。并发WEB应
用通常通过这种做法来获得快速响应。
JNDI - 这个数据源的配置是为了准备与像Spring或应用服务器能够在
外部或者内部配置数据-->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 使用注解时配置javaBean的位置-->
<mapper class="com.evenif.dao.IndexDao"/>
</mappers>
</configuration>
4 Main.java
package com.evenif;
import com.evenif.bean.Index;
import com.evenif.bean.Person;
import com.evenif.dao.IndexDao;
import com.evenif.mybatis.MybatisUtil;
import com.evenif.service.IndexService;
import com.evenif.service.PersonService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
SqlSession session = MybatisUtil.getSession();
IndexDao dao = session.getMapper(IndexDao.class);
{
System.out.println("注解insert:");
dao.insert(new Index(9,"index09"));
}
{
System.out.println("注解getById:");
Index index = dao.getById(9);
System.out.println(index);
}
{
System.out.println("注解delete:");
dao.delete(9);
}
{
System.out.println("注解loadAll():");
List<Index> list = dao.loadAll(0, 100);
System.out.println(list);
}
{
Index index = new Index(8,"index08");
System.out.println("注解update:");
dao.update(index);
}
{
System.out.println("注解loadAll():");
List<Index> list = dao.loadAll(0, 100);
System.out.println(list);
}
}
}
//执行结果:
//注解insert:
//注解getById:
// Index{id=9, name='index09'}
//注解delete:
//注解loadAll():
// [Index{id=2, name='index02'}, Index{id=5, name='index03'}, Index{id=6, name='update'}, Index{id=7, name='index03'}, Index{id=8, name='index03'}]
//注解update:
//注解loadAll():
// [Index{id=2, name='index02'}, Index{id=5, name='index03'}, Index{id=6, name='update'}, Index{id=7, name='index03'}, Index{id=8, name='index08'}]