这里将的是简单的增、删、改、查等基本操作
首先创建java项目,导入相应的依赖包,这里可以参考上一篇博客
1、添加数据
在jike.book.pojo包中,新建java类
JiKeUser.java:
package jike.book.pojo; public class JiKeUser {
private int id;
private String userName;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在jike.book.map中有:
MyBatisConfig.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> <!-- 定义别名 ,必须放在environments之前-->
<typeAliases>
<typeAlias alias="JiKeUser" type="jike.book.pojo.JiKeUser"/>
</typeAliases> <environments default="development">
<environment id="development">
<transactionManager type="JDBC">
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/jikebook"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="jike/book/map/jikeUser.xml"/>
</mappers> </configuration>
jikeUser.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="/"> <select id="findById" parameterType="int" resultType="jike.book.pojo.JiKeUser">
select * from jikeUser where id=#{id}
</select> <insert id="insertUser" parameterType="JiKeUser" statementType="PREPARED"
keyProperty="id" useGeneratedKeys="true">
insert into JikeUser (userName,password) values(#{userName},#{password})
</insert>
</mapper>
新建测试类:
TestUpdate.java
package jike.book.test; import java.io.IOException;
import java.io.Reader; import jike.book.map.InterfaceJiKeUserMap;
import jike.book.pojo.JiKeUser; 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 TestUpdate { public static void main(String[] args) {
String resource = "jike/book/map/MyBatisConfig.xml";
Reader reader = null;
SqlSession session;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
session = sqlMapper.openSession();
try{
/* 添加 */
JiKeUser jku = new JiKeUser();
jku.setUserName("jike001");
jku.setPassword("");
session.insert("insertUser",jku);
session.commit(); }catch(Exception e){
e.printStackTrace();
}finally{
session.close();
}
} }
执行java测试类即可
2、修改
修改数据和添加数据差不多,在jikeUser.xml中添加
<update id="updateUser" parameterType="JiKeUser">
update JiKeUser set userName = #{userName},password=#{password}
where id=#{id}
</update>
然后将测试类改为:
package jike.book.test; import java.io.IOException;
import java.io.Reader; import jike.book.map.InterfaceJiKeUserMap;
import jike.book.pojo.JiKeUser; 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 TestUpdate { public static void main(String[] args) {
String resource = "jike/book/map/MyBatisConfig.xml";
Reader reader = null;
SqlSession session;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
session = sqlMapper.openSession();
try{
/* 修改 */
JiKeUser jku2 = new JiKeUser();
jku2.setUserName("jike00100");
jku2.setPassword("");
jku2.setId();
session.update("updateUser", jku2); session.commit(); }catch(Exception e){
e.printStackTrace();
}finally{
session.close();
}
} }
即可
3、删除
删除操作稍微有些不同,首先在jike.book.map中新建一个接口
InterfaceJiKeUserMap.java,采用注解方式
package jike.book.map; import org.apache.ibatis.annotations.Delete; public interface InterfaceJiKeUserMap { @Delete("delete from JiKeUser where id=#{id}" )
public void deleteUser(Integer id); }
在MyBatisConfig.xml中加入map映射
<mapper class="jike.book.map.InterfaceJiKeUserMap" />
然后修改测试类
package jike.book.test; import java.io.IOException;
import java.io.Reader; import jike.book.map.InterfaceJiKeUserMap;
import jike.book.pojo.JiKeUser; 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 TestUpdate { public static void main(String[] args) {
String resource = "jike/book/map/MyBatisConfig.xml";
Reader reader = null;
SqlSession session;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
session = sqlMapper.openSession();
try{ /* 删除 */
InterfaceJiKeUserMap ijum = session.getMapper(InterfaceJiKeUserMap.class);
ijum.deleteUser();
session.commit(); }catch(Exception e){
e.printStackTrace();
}finally{
session.close();
}
} }
即可
4、查询
查询比较麻烦,分为查询一条数据和查询多条数据
4.1 查询一条语句(两种方式:利用HashMap 和 利用 JiKeUser对象)
4.1.1 利用hashmap实现
在jikeUser.xml下添加语句
<select id="loginSelect" resultType="JiKeUser" parameterType="hashmap">
select * from JiKeUser where userName=#{userName} and password=#{password}
</select>
然后修改测试类
package jike.book.test; import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List; import jike.book.pojo.JiKeUser; 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 TestSelect { public static void main(String[] args) {
String resource = "jike/book/map/MyBatisConfig.xml";
Reader reader = null;
SqlSession session;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
session = sqlMapper.openSession();
try{ /*
* 查询一条记录
* */ /* 利用HashMap */
HashMap<String,String> hm = new HashMap();
hm.put("userName", "jike00100");
hm.put("password", "");
JiKeUser temp = session.selectOne("loginSelect",hm);
if(temp!=null){
System.out.println("登陆成功!");
} }catch(Exception e){
e.printStackTrace();
}finally{
session.close();
}
}
}
4.1.2 利用 JiKeUser对象
在jikeUser.xml下添加语句
<select id="login2" resultType="JiKeUser" parameterType="JiKeUser">
select * from JiKeUser where userName=#{userName} and password=#{password}
</select>
修改测试类:
package jike.book.test; import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List; import jike.book.pojo.JiKeUser; 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 TestSelect { public static void main(String[] args) {
String resource = "jike/book/map/MyBatisConfig.xml";
Reader reader = null;
SqlSession session;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
session = sqlMapper.openSession();
try{ /*
* 查询一条记录
* */ /* 利用 JiKeUser对象 */
JiKeUser jku = new JiKeUser();
jku.setUserName("jike00100");
jku.setPassword("");
JiKeUser onetemp = session.selectOne("login2",jku);
if(onetemp!=null){
System.out.println("登陆成功");
} }catch(Exception e){
e.printStackTrace();
}finally{
session.close();
}
}
}
4.2 查询多条数据(利用resultType 和 利用resultMap)
4.2.1 利用resultType
在jikeUser.xml下添加语句
<select id="selectJiKeUserList" resultType="JiKeUser">
select * from JiKeUser
</select>
修改测试类
package jike.book.test; import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List; import jike.book.pojo.JiKeUser; 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 TestSelect { public static void main(String[] args) {
String resource = "jike/book/map/MyBatisConfig.xml";
Reader reader = null;
SqlSession session;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
session = sqlMapper.openSession();
try{ /*
* 查询多条记录
* */
/* 利用resultType */
List<JiKeUser> ap = session.selectList("selectJiKeUserList");
for(JiKeUser temp:ap){
System.out.println("用户名="+temp.getUserName()+"密码="+temp.getPassword());
} }catch(Exception e){
e.printStackTrace();
}finally{
session.close();
}
}
}
4.2.2 利用resultMap
在jikeUser.xml下添加语句
<resultMap type="JiKeUser" id="JiKeUserMap">
<id property="id" column="id"></id>
<result property="userName" column="userName"/>
<result property="password" column="password"/>
</resultMap>
<select id="selectUsers" resultMap="JiKeUserMap">
select id,userName,password from JiKeUser
</select>
测试类:
package jike.book.test; import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List; import jike.book.pojo.JiKeUser; 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 TestSelect { public static void main(String[] args) {
String resource = "jike/book/map/MyBatisConfig.xml";
Reader reader = null;
SqlSession session;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
session = sqlMapper.openSession();
try{ /*
* 查询多条记录
* */ /* 利用resultMap */
List<JiKeUser> ap1 = session.selectList("selectUsers");
for(JiKeUser temp:ap1){
System.out.println("用户名="+temp.getUserName());
}
}catch(Exception e){
e.printStackTrace();
}finally{
session.close();
}
}
}