1.添加相应的jar包
2.创建持久化类
在src目录下创建一个名为com.mybatis.po的包
创建持久化类MyUser,包含三个属性(uid,uname,usex)
package com.mybatis.po;
/**
*springtest数据库中user表的持久化类
*/
public class MyUser {
private Integer uid;//主键
private String uname;
private String usex;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUsex() {
return usex;
}
public void setUsex(String usex) {
this.usex = usex;
}
@Override
public String toString() {
return "User [uid=" + uid +",uname=" + uname + ",usex=" + usex +"]";
}
}
3.创建映射文件
在src目录下创建一个名为com.mybatis.mapper的包
创建映射文件UserMapper.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.mybatis.mapper.UserMapper">
<!-- 根据uid查询一个用户信息 -->
<select id="selectUserById" parameterType="Integer"
resultType="com.mybatis.po.MyUser">
select * from user where uid = #{uid}
</select>
<!-- 查询所有用户信息 -->
<select id="selectAllUser" resultType="com.mybatis.po.MyUser">
select * from user
</select>
<!-- 添加一个用户 ,#{uname}为com.mybatis.po.MyUser的属性值-->
<insert id="addUser" parameterType="com.mybatis.po.MyUser">
insert into user (uname,usex) values(#{uname},#{usex})
</insert>
<!-- 修改一个用户 -->
<update id="updateUser" parameterType="com.mybatis.po.MyUser">
update user set uname = #{uname},usex = #{usex} where uid = #{uid}
</update>
<!-- 删除一个用户 -->
<delete id="deleteUser" parameterType="Integer">
delete from user where uid = #{uid}
</delete>
</mapper>
4.创建Mybatis的配置文件
在src目录下创建Mybatis的核心配置文件(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>
<!-- 配置环境 -->
<environments default="development">
<environment id="development">
<!-- 使用JDBC的事务管理 -->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- MySQL数据库驱动 -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!-- 连接数据库的URL -->
<property name="url" value="jdbc:mysql://localhost:3306/数据库名?characterEncoding=utf8"/>
<property name="username" value=" 账号名 "/>
<property name="password" value=" 密码 "/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 映射文件的位置 -->
<mapper resource="com/mybatis/mapper/UserMapper.xml"/>
</mappers>
</configuration>
5.创建测试类
在src目录下创建一个名为com.mybatis.test的包,在该包中创建MyBatisTest测试类
package com.mybatis.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 com.mybatis.po.MyUser;
public class MyBatisTest {
public static void main(String[] args) {
try {
//读取配置文件mybatis-config.xml
InputStream config = Resources.getResourceAsStream("mybatis-config.xml");
//根据配置文件构建SqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(config);
//通过SqlSessionFactory创建SqlSession
SqlSession ss = ssf.openSession();
//SqlSession执行映射文件中定义的SQL,并返回映射结果
//com.mybatis.mapper.UserMapper.selectUserById为UserMapper.xml中的命名空间+select的id
//查询一个用户
MyUser mu = ss.selectOne("com.mybatis.mapper.UserMapper.selectUserById", 1);
System.out.println(mu);
//添加一个用户
MyUser addmu = new MyUser();
addmu.setUname("张三");
addmu.setUsex("男");
ss.insert("com.mybatis.mapper.UserMapper.addUser",addmu);
//修改一个用户
MyUser updatemu = new MyUser();
updatemu.setUid(1);
updatemu.setUname("李四");
updatemu.setUsex("女");
ss.update("com.mybatis.mapper.UserMapper.updateUser", updatemu);
//删除一个用户
ss.delete("com.mybatis.mapper.UserMapper.deleteUser", 3);
//查询所有用户
List<MyUser> listMu = ss.selectList("com.mybatis.mapper.UserMapper.selectAllUser");
for (MyUser myUser : listMu) {
System.out.println(myUser);
}
//提交事务
ss.commit();
//关闭SqlSession
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
通过SqlSessionFactory对象创建SqlSwssion对象,并使用SqlSession对象的方法进行执行
数据库操作
调式:
在src目录下创建创建日志文件(log4j.properties)
作用:便于开发者查看控制台输出的SQL语句
代码如下:
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.mybatis=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
效果如下
未创建日志文件如图
6.在数据中创建表
打开数据中对应的 表