一、导包:
oricle和mybatis的包
二、写一个实体类,生成get,set,tostring。
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 configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 引入外部属性文件,datasource里面可以使用${} --> <properties resource="db.properties"></properties> <!-- 定义一个别名组 --> <typeAliases> <!-- <typeAlias type="com.model.TUsers" alias="q"/> --> <!-- package有一个默认值,别名就可以使用类的首字母小写 --> <package name="com.model"/> </typeAliases> <!-- mybatis的环境组 --> <environments default="test"> <environment id="test"> <!-- mybatis支持的事务: JDBC:数据源自己的事务管理 MANAGE(托管),不支持tomcat,一般用在大型的商业服务器,weblogic,jboss --> <transactionManager type="JDBC"> </transactionManager> <!-- 数据库连接 --> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> <property name="username" value="lyh"></property> <property name="password" value="lyh"></property> </dataSource> </environment> </environments> <!-- 把实体类对应的配置文件映射进来 --> <mappers> <mapper resource="com/dao/TuserMapper.xml"></mapper> </mappers> </configuration>
四、写一个实体类的配置文件
<?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.dao.TuserMapper"> <!-- 实体类属性名和列名如果不一致的处理--> <resultMap type="tUsers" id="uses"> <result property="username" column="username"/> </resultMap> <!-- 全表查询 --> <select id="list" resultType="tUsers"> select * from t_users </select> <!-- 添加 --> <insert id="addUser" parameterType="tUsers"> insert into t_users values(sq_mybatis.nextval,#{username},#{password}) </insert> <!-- 删除 --> <delete id="delUser" parameterType="Integer"> delete t_users t where t.id=#{id} </delete> <!-- 修改 --> <update id="updateUser" parameterType="tUsers"> update t_users t set t.username=#{username}, t.password=#{password} where t.id=#{id} </update> </mapper>
五、建一个工具类,用来获取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 getSqlSession(){ InputStream in = null; try { in = Resources.getResourceAsStream("mybatis.xml"); ssf = new SqlSessionFactoryBuilder().build(in); ss = ssf.openSession(); in.close(); } catch (IOException e) { e.printStackTrace(); } return ss; } /* public static void main(String[] args){ System.out.println(getSqlSession()); } */ }
六、新建一个测试用例,测试一package com.uti
import static org.junit.Assert.*; import java.util.List; 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 { ss = MybatisUtil.getSqlSession(); } @After public void tearDown() throws Exception { ss.commit(); } public void test() {//查询所有 List<TUsers> list = ss.selectList("com.dao.TuserMapper.list"); for(TUsers u :list){ System.out.println(u); } } public void addUser(){//添加 TUsers t = new TUsers(); t.setUsername("lihaha"); t.setPassword("123123"); int i = ss.insert("com.dao.TuserMapper.addUser", t); System.out.println(i); } public void delUser(){ //删除 int i = ss.delete("com.dao.TuserMapper.delUser", 2); System.out.println(i); } @Test
public void updateUser(){ //修改 TUsers t = new TUsers(); t.setId(21); t.setUsername("liweixi"); t.setPassword("777777"); int i = ss.update("com.dao.TuserMapper.updateUser", t); System.out.println(i); } }
六、结果显示
查询所有的结果:
添加的结果:
修改的结果:
删除的结果: