1、步骤:
①创建Bean类,及对应的Mapper接口类及其mapper映射文件
②创建及配置Mybatis配置文件,配置数据库信息
③为Mapper接口类创建增删查改对应的方法
④在mapper映射文件中,使用<insert><select><delete><update>
标签配置增删查改的sql
⑤在调用的时候创建SqlSessionFactory和SqlSession对象
⑥创建Bean对象,调用对应的增删查改方法
2、注意事项:
①如果数据库支持自动生成主键,则insert的标签可以使用useGeneratedKeys=“true"并且使用keyProperty指定bean中对应的主键属性名
②如果数据库不支持自动生成主键,则insert标签里面需要使用selectKey标签,把获取seq的sql放到selectKey标签里面,在该标签里面要使用keyProperty指定bean中对应的主键属性名,还可以用order属性设置"BEFORE”/“AFTER”,设定是先获取主键值还是先运行插入数据再获取主键
3、mybatis.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="dbConfig.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="InfoMapper.xml" />
</mappers>
</configuration>
4、InfoMapper.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.demo.ssmtest.InfoMapper">
<insert id="add" parameterType="com.demo.ssmtest.Info" useGeneratedKeys="true" keyProperty="id">
insert into info (name,password,description) values (#{name},#{password},#{description})
</insert>
<delete id="deleteById">
delete from info where id=#{id}
</delete>
<select id="getById" resultType="com.demo.ssmtest.Info">
select * from info where id = #{id}
</select>
<update id="updateById">
update info set name=#{name},password=#{password},description=#{description} where id=#{id}
</update>
</mapper>
5、dbConfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis
jdbc.username=root
jdbc.password=
6、Info.java
package com.demo.ssmtest;
public class Info {
Integer id;
String name;
String password;
String description;
public Info() {
super();
}
public Info(Integer id, String name, String password, String description) {
super();
this.id = id;
this.name = name;
this.password = password;
this.description = description;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Info [id=" + id + ", name=" + name + ", password=" + password + ", description=" + description + "]";
}
}
7、InfoMapper.java
package com.demo.ssmtest;
public interface InfoMapper {
public Integer add(Info info);
public Integer deleteById(int id);
public Info getById(int id);
public Integer updateById(Info info);
}
8、TestMain.java
package com.demo.ssmtest;
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;
public class TestMain {
public static void main(String[] args) throws Exception {
test();
}
public static void test() throws Exception {
String resource = "mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSessionFactory.openSession();
try {
//增
InfoMapper addMapper = openSession.getMapper(InfoMapper.class);
Info add_Info = new Info(null,"nameNew","qweqwe","dddddddd");
int add_Index = addMapper.add(add_Info);
System.out.println(add_Index);
//删
InfoMapper delMapper = openSession.getMapper(InfoMapper.class);
int del_Index = delMapper.deleteById(5);
System.out.println(del_Index);
//查
InfoMapper queryMapper = openSession.getMapper(InfoMapper.class);
Info info = queryMapper.getById(1);
System.out.println(info);
//改
InfoMapper updateMapper = openSession.getMapper(InfoMapper.class);
Info update_Info = new Info(4,"nameNewUpdate","qweqweUpdate","ddddddddUpdate");
int update_Index = updateMapper.updateById(update_Info);
System.out.println(update_Index);
} finally {
openSession.close();
}
}
}
9、项目目录
10、demo
https://download.youkuaiyun.com/download/qq_22778717/10712450