前身是Ibatis,数据持久化的解决方案。
Hibernate是纯ORM,Mybatis是半ORM。
mybatis让程序将主要精力放在sql上,通过mybatis提供的映射方式,自由灵活生成(半自动化,大部分需要程序员编写sql)满足需要sql语句。
1》引入jar包
log4j-1.2.17.jar
mybatis-3.3.1.jar
obdbc6.jar
2》编写数据库的jdbc配置文件mybatis.xml放到src目录下
<?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"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!--表示Oracle数据库链接驱动--> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <!--表示链接字符串--> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/> <property name="username" value="hotelsup"/> <property name="password" value="123"/> </dataSource> </environment> </environments> <mappers> <!--sql的配置文件 暂时不管--> </mappers>
</configuration>
3》编写实体类(数据库表映射的实体类)
package com.jinglin.hotelsup.model; import java.io.Serializable; public class Userinfo implements Serializable{ private Integer userid ; private String username ; private String userpwd ; private String card ; private String job ; private String del ; 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 getCard() { return card; } public void setCard(String card) { this.card = card; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public String getDel() { return del; } public void setDel(String del) { this.del = del; } }
4》编写SQL配置文件。表示对于这张映射的表的所有SQL操作都放在这个配置文件里,而这个配置文件存放的路径就是model所在的路径。(SQL的语句写在配置文件里,后期还有数据映射的配置文件)
(编写UserinfoMapper.xml放置在数据库实体映射的model层下)
<?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.jinglin.hotelsup.model"> <!-- 编写SQL语句,select,delete,update,insert --> <insert id="insertitem" parameterType="com.jinglin.hotelsup.model.Userinfo"> insert into userinfo(userid,username,userpwd,card,job) values(userseq.nextval,#{username},#{userpwd},#{card},#{job}) </insert> <update id="updateitem" parameterType="com.jinglin.hotelsup.model.Userinfo"> update userinfo set username=#{username},userpwd=#{userpwd},card=#{card},job=#{job} where userid=#{userid} </update> <!--物理删除--> <delete id="deleteitem" > delete userinfo where userid=#{id} </delete> <!--逻辑删除--> <delete id="updateuser" parameterType="com.jinglin.hotelsup.model.Userinfo"> update userinfo set del=‘Y‘ where userid=#{id} </delete> <select id="selectone" resultType="com.jinglin.hotelsup.model.Userinfo"> select * from userinfo where del=‘N‘ and userid=#{id} </select> <select id="selectall" resultMap="com.jinglin.hotelsup.model.userinfomap"> select * from userinfo where del=‘N‘ </select> <!-- 配置查询结果集 数据表字段和实体类的属性的映射关系--> <resultMap id="userinfomap" type="com.jinglin.hotelsup.model.Userinfo"> <!-- id表示首先配置主键 ,property表示的是实体类的属性名,column表示的是数据库表的字段名--> <id property="userid" column="userid"/> <result property="username" column="username"/> <result property="userpwd" column="userpwd"/> <result property="card" column="card"/> <result property="job" column="job"/> <result property="del" column="del"/> </resultMap> </mapper>
5》将该SQL配置文件加入到mybatis-config.xml里。(2中sql的配置文件 暂时不管的内容)
<mappers> <!-- 引入映射的配置文件 --> <mapper resource="com/jinglin/bookstore/model/UserinfoMapper.xml" /> </mappers>
6》操作mybatis,首先需要获取SqlSessionFactory对象,由这个对象来生成SqlSession(表示每次与数据库的交互):
package com.jinglin.hotelsup.dao.imp; 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 org.junit.Test; import com.jinglin.hotelsup.dao.IDao; import com.jinglin.hotelsup.model.Userinfo; public class UserInfoDao implements IDao<Userinfo> { static SqlSessionFactory sqlsessionFactory =null; static{ String resource = "mybatis-config.xml"; try { //把配置信息读取到内存中 InputStream inputStrem =Resources.getResourceAsStream(resource); //创建sqlsessionFactory这个对象 sqlsessionFactory =new SqlSessionFactoryBuilder().build(inputStrem); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public int additem(Useriinfo t) { //采用Mybatis的方法 SqlSession sqlSession = sqlsessionFactory.openSession(); //表示调用新增数据的方法 int i =sqlSession.insert("com.jinglin.hotelsup.model.insertItem", t); //涉及到事务 sqlSession.commit();//把操作的数据提交到数据库中 //关掉操作 sqlSession.close(); return i; } @Test public void testit(){ Userinfo u = new Userinfo(); u.setCard("123"); u.setJob("工程师"); u.setUsername("admin123"); u.setUserpwd("123456"); int result = additem(u); System.out.println(result); } }
时间: 2024-10-08 17:36:49