原始的JDBC太过繁琐,因此出现大量的数据库连接框架,Mybatis就是其中之一
优点:简单易学、简化代码量、SQL写于XML中
代码
1、创建web应用,导入jar包
链接:https://pan.baidu.com/s/1uxLt_FUr2i2v7T7oGMUPLA
提取码:nrji
创建数据库
create database how2java;
创建表
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;
插入数据
INSERT INTO category_ VALUES (null,'category1');
INSERT INTO category_ VALUES (null,'category2');
2、src下创建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>
<!-- 扫描model -->
<typeAliases>
<package name="com.llhc.model"/>
</typeAliases>
<!-- 连接jdbc -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis1?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<mapper resource="com/llhc/model/Category.xml"/>
</mappers>
</configuration>
3、Category
package com.llhc.model;
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;
}
}
4、category.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.llhc.model">
<insert id="addCategory" parameterType="Category" >
insert into category_ ( name ) values (#{name})
</insert>
<delete id="deleteCategory" parameterType="Category" >
delete from category_ where id= #{id}
</delete>
<select id="getCategory" parameterType="_int" resultType="Category">
select * from category_ where id= #{id}
</select>
<update id="updateCategory" parameterType="Category" >
update category_ set name=#{name} where id=#{id}
</update>
</mapper>
5、controller
package com.llhc.controller;
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 com.llhc.model.Category;
public class TestMybatis {
public static void main(String[] args) throws IOException {
System.out.println("fasdfda");
//1、连接配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
/*2、添加
session.insert("addCategory",CategoryAdd());*/
/*3、删除
session.delete("deleteCategory",CategoryDelete());*/
/*4、查询
Category c=session.selectOne("getCategory",2);
System.out.println(c.getName());*/
/* 5、修改
session.update("updateCategory",CategoryUpdate(session));*/
}
//添加
public static Category CategoryAdd(){
//执行添加操作
Category c = new Category();
c.setName("Categoryhehe");
return c;
}
//删除
public static Category CategoryDelete(){
//执行删除操作
Category c = new Category();
c.setId(3);
return c;
}
//修改
public static Category CategoryUpdate(SqlSession session){
Category c=session.selectOne("getCategory",1);
c.setName("修改成功");
return c;
}
}
6、执行测试即可!
图片.png
网友评论