如果使用 Maven 来构建项目,则需将下面的 dependency 代码置于 pom.xml 文件中:
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>x.x.xversion>
dependency>
我的version版本是3.5.3
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.18version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>utf-8encoding>
configuration>
plugin>
plugins>
build>
use test;
CREATE TABLE category_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(32) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
username=root
password=root
<configuration>
<properties resource="db.properties">properties>
<typeAliases>
<package name="pojo"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<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>
<mapper resource="pojo/Category.xml"/>
mappers>
configuration>
package pojo;
public class Category {
private int id;
private String name;
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;
}
}
<mapper namespace="pojo">
<select id="list" resultType="Category">
select * from category_
select>
<select id="one" parameterType="int" resultType="Category">
select * from category_ where id=#{id}
select>
<insert id="save" parameterType="Category">
insert into category_ values(null,#{name})
insert>
<delete id="delete" parameterType="int">
delete from category_ where id=#{id}
delete>
<update id="update" parameterType="Category">
update category_ set name=#{name} where id=#{id}
update>
mapper>
package test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
import pojo.Category;
public class TestCase {
SqlSessionFactory sf;
SqlSession ss;
@Before
public void before() throws IOException {
String resource="mybatis.cfg.xml";
//配置文件mybatis-config.xml,得到SqlSessionFactory
InputStream inputStream=Resources.getResourceAsStream(resource);
sf=new SqlSessionFactoryBuilder().build(inputStream);
//通过SqlSessionFactory获得SqlSession
ss=sf.openSession();
}
/*
*查询所有
*/
@Test
public void test() {
List<Category>list=ss.selectList("list");
for (Category category : list) {
System.out.println(category.getName());
}
}
/*
*新增数据
*/
@Test
public void save() {
Category c=new Category();
c.setName("category 1");
int count=ss.insert("save",c);
System.out.println(count);//返回新增的数据的个数
}
/*
*修改数据
*/
@Test
public void update(){
Category c=ss.selectOne("one", 1);
c.setName("分类1");
int count=ss.update("update", c);
System.out.println(count);//返回更新的数据的个数
}
/*
*删除数据
*/
@Test
public void delete(){
Category c=ss.selectOne("one", 1);
int count=ss.delete("delete",c);
System.out.println(count);//返回删除数据的个数
}
//SqlSession的关闭
@After
public void after() {
// 提交事务
ss.commit();
// 关闭Session
ss.close();
}
}
应用程序找Mybatis要数据
mybatis从数据库中找来数据
通过mybatis-cfg.xml 定位哪个数据库
通过Category.xml执行对应的select,update,insert,delete语句
基于Category.xml把返回的数据库记录封装在Category对象中
返回数据