mybatis开发过程中遇到的问题

自己最近听说了一个比hibernate更方便的的ORM映射框架 -- Mybatis,,自己网上查资料后就写了一个测试程序!! 了解了mybatis的开发流程 ,,下面我就写个简单的例子


第一步model层

User.java

public class User {
private String username;

public String getUsername() {
return username;
}


public void setUsername(String username) {
this.username = username;
}


private String password;



public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


}


第二步 mapper 层


public interface UserMapper {
public void add(User user);

public void delete(String password);

public List<User> list();

public void update(String username, String password);

public User load(String username);
}


第三步 映射文件


<?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.ouma.mapper.User"> -->

<!--面向接口的编程,当然你可以不使用这种,使用上面的那种-->
  <mapper namespace="com.ouma.mapper.UserMapper">
  <insert id="add" parameterType="user">
  insert into t_user(username,password) value(#{username}, #{password})
  </insert>
 
  <delete id="delete" parameterType="java.lang.String">
delete from t_user where password=#{password}
  </delete>
 
  <select  id="list" resultType="User">
  select * from t_user
  </select>
 
  <update id="update" parameterType="String">
  update t_user set password=#{password} where username=#{username}
  </update>
 
  <select id="load" parameterType="String">
  select * from t_user where username=#{username}
  </select>
  </mapper>


第四步 关键的配置文件


<?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="jdbc.properties" /> -->
<typeAliases>
<typeAlias type="com.ouma.entity.User" alias="user"/>
</typeAliases>
  <environments default="development">
  <environment id="development">
  <transactionManager type="JDBC" />
  <dataSource type="POOLED" >
  <property name="driver" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
  <property name="username" value="root"/>
  <property name="password" value="root"/>
  </dataSource>
  </environment>
  </environments>
  <mappers>
  <mapper resource="com/ouma/entity/User.xml"></mapper>
  </mappers>
 </configuration>


第五部 测试类

工具类


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;


import com.mysql.jdbc.interceptors.SessionAssociationInterceptor;


public class GetSession {

static SqlSessionFactory sessionFactory = null;

static {
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream("mybatils-conf.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}

}

public static SqlSession getSession() {
return sessionFactory.openSession();
}

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


测试类

import java.util.List;


import junit.framework.TestCase;


import org.apache.ibatis.session.SqlSession;


import com.ouma.entity.User;
import com.ouma.mapper.UserMapper;
import com.ouma.util.GetSession;


public class TestMybatis extends TestCase{



public void test1() {
SqlSession SqlSession = GetSession.getSession();
User user = new User();
user.setUsername("张三");
user.setPassword("1234");
SqlSession.getMapper(UserMapper.class).add(user);
SqlSession.commit();
GetSession.closeSqlSession(SqlSession);

}

public void testDelete() {
SqlSession sqlSession = GetSession.getSession();
sqlSession.getMapper(UserMapper.class).delete("123");
sqlSession.commit();
GetSession.closeSqlSession(sqlSession);

}

public void testList() {
SqlSession sqlSession = GetSession.getSession();
List<User> users = sqlSession.getMapper(UserMapper.class).list();
System.out.println(users.size());
sqlSession.commit();
GetSession.closeSqlSession(sqlSession);

}



public void testUpdate() {
SqlSession session = GetSession.getSession();
//User user = session.getMapper(UserMapper.class).load("张三");
session.getMapper(UserMapper.class).update("张三", "1");
session.commit();
GetSession.closeSqlSession(session);
}
}


以上是本人自己总结的有不对的地方还请提出问题来,大家共同讨论。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值