MyBatis框架

MyBatis框架




1、导入四个包到lib下


①log4j-1.2.16.jar
②mybatis-3.2.2-sources.jar
③mybatis-3.2.2.jar
④mysql-connector-java-5.1.0-bin.jar

1.1在src下导入2个文件,一个xml  database.properties     log4j.properties   mybatis-config.xml


2、在src下创建一个实体类


注意:该实体类不能创建带参构造器


package com.bdqn.entity;


import java.io.Serializable;


public class User implements Serializable{

private int uid;
private String uname;
private String udesc;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUdesc() {
return udesc;
}
public void setUdesc(String udesc) {
this.udesc = udesc;
}
@Override
public String toString() {
return "User [uid=" + uid + ", uname=" + uname + ", udesc=" + udesc
+ "]";
}

}


3、在src下dao包下创建UserDao
package com.bdqn.dao;


import java.util.List;

import com.bdqn.entity.User;

public interface UserDao {

public List<User> selectAllUser();

public int deleteUser(int uid);

public int updateUser(User user);

//模糊查询
public List<User> selectLikeUser(String uname);

//增加

public int insertUser(User user);
}


4、在src的dao包下导入一个xml文件,然后对xml文件进行业务处理   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.bdqn.dao.UserDao">

<select id="count" resultType="int">
select count(1) from user;
</select>

<select id="selectAllUser" resultType="com.bdqn.entity.User">
select * from user;
</select>

<select id="selectLikeUser" resultType="com.bdqn.entity.User" parameterType="String">
select * from user where uname like concat('%',#{uname},'%');
</select>

<delete id="deleteUser" parameterType="com.bdqn.entity.User">
delete from user where uid=#{uid};
</delete>

<update id="updateUser" parameterType="com.bdqn.entity.User">
update user set uname=#{uname},udesc=#{udesc} where uid=#{uid}
</update>

<insert id="insertUser" parameterType="com.bdqn.entity.User">
insert into user(uid,uname,udesc) values(#{uid},#{uname},#{udesc})
</insert>
</mapper>

5、工具类,调用MyBtis包中的方法,获取JDBC的代码实现

package com.bdqn.utils;


import java.io.IOException;
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 SqlSessionUtils {

private static SqlSessionFactory sqlSessionFactory;

static {
try {
//通过输入读取配置文件
InputStream is = Resources
.getResourceAsStream("mybatis-config.xml");
//创建一个数据库会话工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
}

public static SqlSession getSqlSession() {
//false取消默认方法的事物提交(打开一次数据库会话)
return sqlSessionFactory.openSession(false);
}

public  static void close(SqlSession sqlSession) {
if (sqlSession != null) {
sqlSession.close();
}
}
}




6、测试

package com.bdqn.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.bdqn.dao.UserDao;
import com.bdqn.entity.User;
import com.bdqn.utils.SqlSessionUtils;

public class Test {

public static void main(String[] args) {
/*InputStream is = null;

try {
is = Resources.getResourceAsStream("mybatis-config.xml");

SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
.build(is);

SqlSession openSession = sessionFactory.openSession(false);
int count = openSession.selectOne("com.bdqn.dao.UserMapper.count");

System.out.println(count);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}*/

SqlSession sqlSession = SqlSessionUtils.getSqlSession();
//查询条数
/*int num = sqlSession.selectOne("com.bdqn.dao.UserMapper.count");
System.out.println(num);*/
//查询所有
/*List<User> list = sqlSession.getMapper(UserDao.class).selectAllUser();

for (User user : list) {

System.out.println(user.toString());
}*/


//删除

/*try {
int num = sqlSession.getMapper(UserDao.class).deleteUser(1);
//必须手动提交,否则删除不成功
sqlSession.commit();
System.out.println(num);
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
}*/

//修改
/*User user = new User();
user.setUid(2);
user.setUname("付佳伟");
user.setUdesc("1122341445");
try {
int num = sqlSession.getMapper(UserDao.class).updateUser(user);
sqlSession.commit();
System.out.println(num);
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
}*/

//模糊查询

/*List<User> list = sqlSession.getMapper(UserDao.class).selectLikeUser("佳");
for (User user : list) {
System.out.println(user.toString());
}*/

//增加
User user=new User();
user.setUid(6);
user.setUname("askj");
user.setUdesc("dgerht");
try {
int num = sqlSession.getMapper(UserDao.class).insertUser(user);
sqlSession.commit();
System.out.println(num);
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值