一、MyBatils 在映射文件中编写sql
1.建库,建表 编写对应实体类
CREATE TABLE `tbl_cat` (
`id` INT ( 10 ) NOT NULL AUTO_INCREMENT,
`catName` VARCHAR ( 20 ) DEFAULT NULL,
`age` INT ( 11 ) DEFAULT NULL,
`birth` DATE DEFAULT NULL,
PRIMARY KEY ( `id`)
) ;
INSERT INTO tbl_cat ( catName, age, birth) VALUES ( 'whiteCat' , 7 , CURRENT_DATE ( ) ) ;
INSERT INTO tbl_cat ( catName, age, birth) VALUES ( 'blackCat' , 5 , CURRENT_DATE ( ) ) ;
INSERT INTO tbl_cat ( catName, age, birth) VALUES ( 'yellowCat' , 3 , CURRENT_DATE ( ) ) ; `tbl_cat`
package cn. yhy. entity;
import java. util. Date;
public class Cat {
private Integer id;
private String catName;
private int age;
private Date birth;
public Cat ( ) { }
public Integer getId ( )
{
return id;
}
public void setId ( Integer id)
{
this . id = id;
}
public String getCatName ( )
{
return catName;
}
public void setCatName ( String catName)
{
this . catName = catName;
}
public int getAge ( )
{
return age;
}
public void setAge ( int age)
{
this . age = age;
}
public Date getBirth ( )
{
return birth;
}
public void setBirth ( Date birth)
{
this . birth = birth;
}
@Override
public String toString ( )
{
return "Cat [id=" + id + ", catName=" + catName + ", age=" + age
+ ", birth=" + birth + "]" ;
}
}
2.编写CRUD接口 xxxMapper
public interface CatMapper {
public Cat getCat ( Cat cat) ;
public List< Cat> getDim ( String name) ;
public int getInsert ( Cat cat) ;
public int getUpdate ( Cat cat) ;
public int getDelete ( int id) ;
}
3.创建sql的映射文件 xxxMapper
< ? 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= "cn.yhy.mapper.CatMapper" >
< ! -- 通过id和姓名查询-- >
< select id= "getCat" parameterType= "Cat" resultType= "Cat" >
select * from tbl_cat where id= #{ id} and catName= #{ catName}
< / select>
< ! -- 模糊查询的第一种方式-- >
< ! -- < select id= "getDim" parameterType= "Cat" resultType= "Cat" > -- >
< ! -- select * from tbl_cat where catName like #{ catName} -- >
< ! -- < / select> -- >
< ! -- 模糊查询的第二种方式-- >
< select id= "getDim" parameterType= "Cat" resultType= "Cat" >
select * from tbl_cat where catName like '%${value}%'
< / select>
< ! -- 添加信息-- >
< insert id= "getInsert" parameterType= "Cat" >
insert into tbl_cat ( catName, age, birth) values ( #{ catName} , #{ age} , #{ birth} )
< / insert>
< ! -- 修改信息-- >
< update id= "getUpdate" parameterType= "Cat" >
update tbl_cat set catName= #{ catName} where id= #{ id}
< / update>
< ! -- 删除-- >
< delete id= "getDelete" parameterType= "Cat" >
delete from tbl_cat where id= #{ id}
< / delete>
< / mapper>
4.在myBatis-config.xml中注册映射文件
< mappers>
< mapper resource= "static/CatMapper.xml" / >
< ! -- 写入编写sql的xml文件,注意路径问题-- >
< mapper resource= "static/PersonMapper.xml" / >
< ! -- 注解方式-- >
< mapper class = "cn.yhy.mapper.PersonMapperAnnotation" > < / mapper>
< ! -- Cat 注解方式增删改查-- >
< mapper class = "cn.yhy.mapper.CatMapperAnnotation" > < / mapper>
< / mappers>
5.测试类测试 通过session.getMapper(CatMapperAnnotation.class);
public class TestCatMapper {
static SqlSessionFactory sqlSessionFactory= null;
static {
String resource = "mybatis-config.xml" ;
InputStream inputStream = null;
try {
inputStream = Resources. getResourceAsStream ( resource) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
sqlSessionFactory= new SqlSessionFactoryBuilder ( ) . build ( inputStream) ;
}
public static void main ( String[ ] args) {
SqlSession session= sqlSessionFactory. openSession ( ) ;
try {
CatMapper mapper = session. getMapper ( CatMapper. class ) ;
int delete = mapper. getDelete ( 3 ) ;
System. out. println ( delete) ;
session. commit ( ) ;
} finally {
session. close ( ) ;
}
}
二、MyBatis以注解的方式增删改查
1.建库 建表
2.编写CRUD接口 xxxMapper
public interface CatMapperAnnotation {
@Select ( "select * from tbl_cat where id=#{id} and catName=#{catName}" )
public Cat getCat ( Cat cat) ;
@Select ( "select * from tbl_cat where catName like '%${value}%'" )
public List< Cat> getDim ( String name) ;
@Insert ( "insert into tbl_cat(catName,age,birth) values (#{catName},#{age},#{birth})" )
public int getInsert ( Cat cat) ;
@Update ( "update tbl_cat set catName=#{catName} where id=#{id}" )
public int getUpdate ( Cat cat) ;
@Delete ( "delete from tbl_cat where id=#{id}" )
public int getDelete ( int id) ;
}
3.在myBatis-config.xml中注册该接口
< mappers>
< mapper resource= "static/CatMapper.xml" / >
< ! -- 写入编写sql的xml文件,注意路径问题-- >
< mapper resource= "static/PersonMapper.xml" / >
< ! -- 注解方式-- >
< mapper class = "cn.yhy.mapper.PersonMapperAnnotation" > < / mapper>
< ! -- Cat 注解方式增删改查-- >
< mapper class = "cn.yhy.mapper.CatMapperAnnotation" > < / mapper>
< / mappers>
4.在测试类中测试
public class TestCatAnnotation {
static SqlSessionFactory sqlSessionFactory= null;
static {
String resource = "mybatis-config.xml" ;
InputStream inputStream = null;
try {
inputStream = Resources. getResourceAsStream ( resource) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
sqlSessionFactory= new SqlSessionFactoryBuilder ( ) . build ( inputStream) ;
}
public static void main ( String[ ] args) {
SqlSession session= sqlSessionFactory. openSession ( ) ;
try {
CatMapperAnnotation mapper = session. getMapper ( CatMapperAnnotation. class ) ;
int delete = mapper. getDelete ( 4 ) ;
System. out. println ( delete) ;
session. commit ( ) ;
} finally {
session. close ( ) ;
}
}
三、MyBatis以API的方式增删改查
1.建表 编写实体类 和 普通的一样
2.创建接口 编写CRUD功能 和普通的一样
3.映射文件编写sql 和 普通的一样
4.myBatis-config.xml文件中映射sql文件 和普通的一样
5.测试类 测试结果 直接使用session自带的增删改查进行处理
public class TestCatMapperAPI {
static SqlSessionFactory sqlSessionFactory= null;
static {
String resource = "mybatis-config.xml" ;
InputStream inputStream = null;
try {
inputStream = Resources. getResourceAsStream ( resource) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
sqlSessionFactory= new SqlSessionFactoryBuilder ( ) . build ( inputStream) ;
}
public static void main ( String[ ] args) {
SqlSession session= sqlSessionFactory. openSession ( ) ;
try {
List< Cat> list = session. selectList ( "cn.yhy.mapper.CatMapperAPI.getCatAll" ) ;
for ( Cat cat : list) {
System. out. println ( cat) ;
}
session. commit ( ) ;
} finally {
session. close ( ) ;
}
}