Mabatis接口之Mapper接口开发

 Mybatis数据操作

1.通过外部配置文件做mybatis的基础性的配置。

1)先编写config.properties的文件(文件1建在src下)(做一些动态的配置)。

配置的内容如下:

jdbc.jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:orcl

jdbc.driverClass=oracle.jdbc.driver.OracleDriver

jdbc.user=bbs

jdbc.password=123

2.在mybatis-config.xml文件2建在src下文件下引入config.properties文件。

<?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>

  <propertiesresource="config.properties"><!—引入外部的配置文件-->

  </properties>

 <environments default="development">

   <environment id="development">

     <transactionManager type="JDBC"/>

     <dataSource type="POOLED">

       <property name="driver" value="${jdbc. driverClass }"/>

       <property name="url" value="${jdbc. jdbcUrl }"/>

       <property name="username"value="${jdbc.user}"/>

       <property name="password"value="${jdbc.password}"/>

     </dataSource>

   </environment>

 </environments>

 <mappers>

    <mapper resource="com/gxa/bj/model/UserInfoMapper.xml"/>

 </mappers>

</configuration>

3)做一个SQL映射文件:

2.Mapper接口代理的实现。

1)先定义一个接口(主要针对数据库表操作的接口)。比如:

package com.gxa.bj.dao;文件3建在src下新建的包

import java.util.List;

/**

 * 针对数据库的数据的增删改查的功能做的接口

 *@author ZY

 *

 */

public interface IDaoBBS<T>{

   /**

    * 向数据库表里添加一条数据的方法

    * @param t

    * @throws SQLException

    * @throws IllegalAccessException

    * @throws IllegalArgumentException

    */

   public int  addItem(T t);

   /**

    * 从数据库表里删除一条数据的方法

    * @param objId

    */

   public int removeItem(Object objId);

   /**

    * 更新数据库表里的一条字段的方法

    * @param t

    */

   public int updateItem(T t);

   

   /**

    * 根据主键字段获取该条数据,并转换成实体对象

    * @param objId

    * @return

    * @throws InvocationTargetException

    * @throws IllegalArgumentException

    * @throws IllegalAccessException

    * @throws SecurityException

    * @throws NoSuchMethodException

    */

   public T getModel(Object objId) ;

   

   /**

    * 根据查询条件获取多条数据,并转换成相应的集合

    * @param objects

    * @return

    * @throws InvocationTargetException

    * @throws IllegalArgumentException

    * @throws IllegalAccessException

    * @throws SecurityException

    * @throws NoSuchMethodException

    */

   public List<T> getList(T t);

}

再针对该接口做一个针对UserInfo表的实现的接口:

package com.gxa.bj.dao.imp;文件4建在src下新建的包

import com.gxa.bj.dao.IDaoBBS;

import com.gxa.bj.model.UserInfo;

 

public interface UserMapperextends IDaoBBS<UserInfo> {

//模糊查询,查询到集合数据

    public List<UserInfo> getUsers(String name);

}

2)在SQL的映射的xml文件里做如下配置

UserInfoMapper.xml(文件5建在src下新建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.gxa.bj.dao.imp.UserMapper">

  <insert id="addItem"parameterType="com.gxa.bj.model.UserInfo">

      insert intoUserInfo(userid,username,userpwd,useremail,useraddress,regreason)

      values(usernext.nextval,#{userName},#{userPwd},#{userEmail},#{userAddress},#{regReason})

  </insert>

  <delete id="removeItem">

       delete from UserInfo where userId=#{id}

  </delete>

<update id="updateItem" parameterType="com.gxa.bj.model.UserInfo">
      update UserInfo set userName=#{userName},userPwd=#{userPwd},userEmail=#{userEmail},userAddress=#{userAddress},regReason=#{regReason} where userId=#{userId}
  </update>
  <select id="getModel" resultType="com.gxa.bj.model.UserInfo">
  select * from userinfo where userid=#{id}
  </select>
  <select id="getUsers" parameterType="String" resultType="com.gxa.bj.model.UserInfo">
  select * from userinfo where userName like '%${value}%'
  </select>

</mapper>

注意:namespace里是所针对的接口的名称(包括包名)。

     接口里的方法全部定义为SQL语句的ID名。比如接口里有一个叫做addItem的方法,那么在SQL语句里就有一个id名为addItem的sql语句。

文件6建在src下新建的包,包下面建实体对象类,例如userinfo用户类titleinfo,typeinfo,replyinfo

com.gxa.bj.model


下面是项目基本包文件截图

下面是com.gxa.bj.model下的类

例如用户类

package com.gxa.bj.model;


public class UserInfo {
private Integer userId;//用户id
private String userName;//用户姓名
private String userPwd;//用户密码
private String userEmail;//用户邮箱
private String regReason;//注册原因
private String userAddress;//用户住址
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getRegReason() {
return regReason;
}
public void setRegReason(String regReason) {
this.regReason = regReason;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}

}

测试方法事例

TestUI.java类

package com.gxa.bj.dao.imp;


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.gxa.bj.model.UserInfo;


public class TestUI  {

public static void main(String[] args){
TestUI t=new TestUI();
//增加
// UserInfo u=new UserInfo();
// u.setUserName("小米");
// u.setUserPwd("222");
// u.setUserEmail("123@qq.com");
// u.setUserAddress("成都");
// u.setRegReason("想加入");
// try {
// t.addUserInfo(u);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//删除
// try {
// t.deleteUserInfo(41);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//更新
// UserInfo u=new UserInfo();
// u.setUserId(42);
// u.setUserName("xiaoqiang");
// u.setUserPwd("1234");
// u.setUserEmail("ww@qq.com");
// u.setUserAddress("重庆");
// u.setRegReason("??");
// try {
// t.updateUserInfo(u);
//
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//根据id查询
// try {
// t.getModel(42);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
try {
t.getList("小");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addUserInfo(UserInfo u) throws IOException{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); 
SqlSession sqlSession=sqlSessionFactory.openSession();
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
if(userMapper.addItem(u)>0){
sqlSession.commit();
System.out.println("添加成功");
}else{
sqlSession.rollback();
System.out.println("添加失败");
}
sqlSession.close();
}
public void deleteUserInfo(int id) throws IOException{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); 
SqlSession sqlSession=sqlSessionFactory.openSession();
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
if(userMapper.removeItem(id)>0){
sqlSession.commit();
System.out.println("删除成功");
}else{
sqlSession.rollback();
System.out.println("删除失败");
}
sqlSession.close();
}
public void updateUserInfo(UserInfo u) throws IOException{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
if(userMapper.updateItem(u)>0){
sqlSession.commit();
System.out.println("更新成功");
}else {
sqlSession.rollback();
System.out.println("更新失败");
}
sqlSession.close();
}
public void getModel(Object objId) throws IOException{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
UserInfo us=userMapper.getModel(objId);
System.out.println(us.getUserName());
System.out.println(us.getUserEmail());
System.out.println(us.getUserPwd());
System.out.println(us.getUserAddress());
System.out.println(us.getRegReason());
}
public void getList(String name) throws IOException{
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
List<UserInfo> list=userMapper.getUsers(name);
for(UserInfo i:list){
System.out.println(i.getUserName()+i.getUserEmail()+i.getUserAddress()+i.getRegReason());
}

}
}

实现基本的增删改查


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值