使用mybatis实例
一、导入.jar包
需要mybatis的和数据库的包,这里用的oracle
二、写配置文件
名字自己起,没有特殊要求
<?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> <!-- 定义一个别名 --> <typeAliases> <package name="com.model"/><!-- 这个包里的类,跟自动装配很想,类首字母小写 --> </typeAliases> <!-- mybatis的环境 --> <environments default="test"> <environment id="test"> <!-- mybatis支持的事务: JDBC: 数据源自己的那个事务管理 MANAGE(托管): 不支持tomcat, 一般用在大型商业服务器, weblogic, jboss --> <transactionManager type="JDBC"/> <!-- 数据源连接类型:POOLED, UNPOOLED, JNDI使用应用服务器的数据源连接 POOLED:连接池--> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/> <property name="username" value="test1116"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 实体类映射 --> <mappers> <mapper resource="com/model/TUsersMapper.xml"/> </mappers> </configuration>
三、添加实体类
要跟数据库对应,可以用hibernate自动生成。
package com.model; public class TUsers { private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer 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; } @Override public String toString() { return "TUsers [id=" + id + ", username=" + username + ", password=" + password + "]"; } }
四、写实体类的映射配置
名字也是自己定义,没有特殊要求
<?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="aaa"> <!-- 全表查询 --> <select id="list" resultType="com.model.TUsers"> select * from t_users </select> <!-- 添加 --> <insert id="add" parameterType="tUsers"><!-- 前面定义了别名,可用首字母小写类名 --> insert into t_users values(test.nextval,#{username},#{password}) </insert> <!-- 删除 --> <update id="delete" parameterType="tUsers"> delete t_users t where t.id=#{id} </update> <!-- 修改 --> <update id="update" parameterType="tUsers"> update t_users t set t.username=#{username},t.password=#{password} where t.id=#{id} </update> <!-- 用map来条件查询 --> <resultMap type="tUsers" id="users"/> <select id="select" parameterType="Map" resultMap="users"> select * from t_users t where t.username = #{p1} and t.password = #{p2} </select> </mapper>
五、用Junit,测试
这里还有个工具类生成SqLSessionFactory和SqlSession
package com.util; 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 MybatisUtil { private static SqlSessionFactory ssf; private static SqlSession ss; public static SqlSession getSession(){ InputStream in=null; try { in=Resources.getResourceAsStream("mybatis_cfg.xml"); ssf=new SqlSessionFactoryBuilder().build(in); ss=ssf.openSession(); in.close(); } catch (IOException e) { e.printStackTrace(); } return ss; } }
junit
package com.util; import static org.junit.Assert.*; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.model.TUsers; public class JTest { private SqlSession ss; @Before public void setUp() throws Exception { //先获取session ss=MybatisUtil.getSession(); } @After public void tearDown() throws Exception { //最后提交和关闭session ss.commit(); ss.close(); } //全表查询 public void test() { List<TUsers> list=ss.selectList("aaa.list"); for(TUsers u:list){ System.out.println(u); } } //添加 public void add() { TUsers us=new TUsers(); us.setUsername("张三"); us.setPassword("33"); int u=ss.insert("aaa.add",us ); System.out.println(u); } //删除 public void delete() { TUsers us=new TUsers(); us.setId(2); int u=ss.delete("aaa.delete",us); System.out.println(u); } //修改 public void update() { TUsers us=new TUsers(); us.setId(1); us.setUsername("张三"); us.setPassword("333"); int u=ss.update("aaa.update",us); System.out.println(u); } //条件查询 @Test public void select() { Map<String, Object> map = new HashMap<String, Object>(); map.put("p1", "赵六"); map.put("p2", "6666"); List<TUsers> list = ss .selectList("aaa.select", map); for(TUsers u : list) { System.out.println(u); } } }
条件查询结果
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/16983.html