微前端架构如何改变企业的开发模式与效率提升
613
2022-10-08
Mybatis入门程序
1.在oracle数据库中新建一个表:
create table t_user( t_id number primary key, t_name varchar2(20), t_password varchar2(20) ); insert into t_user values(1,'tom','123456'); insert into t_user values(2,'link','123456'); insert into t_user values(3,'sony','123456'); insert into t_user values(4,'linux','123456'); commit;
2.项目文件介绍
3.Tuser.java;
package com.eduask.pojo; public class Tuser { private int t_id; private String t_name; private String t_password; public int getT_id() { return t_id; } public void setT_id(int t_id) { this.t_id = t_id; } public String getT_name() { return t_name; } public void setT_name(String t_name) { this.t_name = t_name; } public String getT_password() { return t_password; } public void setT_password(String t_password) { this.t_password = t_password; } public Tuser(int t_id, String t_name, String t_password) { super(); this.t_id = t_id; this.t_name = t_name; this.t_password = t_password; } public Tuser() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Tuser [t_id=" + t_id + ", t_name=" + t_name + ", t_password=" + t_password + "]"; } }
4.UserTest.java
package com.eduask.test; 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.eduask.pojo.Tuser; public class UserTest { //mybatis的用户的增加; @Test public void selectUserById() throws IOException { //将配置文件转换为字节流 InputStream is=Resources.getResourceAsStream("configuration.xml"); //创建SqlSessionFactory工厂 SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is); //创建SqlSession(打开了一个会话) SqlSession session=sessionFactory.openSession(); Tuser tuser=session.selectOne("selectOneUsers", 3); System.out.println("查询id=3"); System.out.println(tuser); session.close(); } //mybatis的用户的增加; @Test public void insertUserById() throws IOException { //将配置文件转换为字节流 InputStream is=Resources.getResourceAsStream("configuration.xml"); //Reader is=Resources.getResourceAsReader("configuration.xml"); //创建SqlSessionFactory工厂 SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is); //创建SqlSession(打开了一个会话) SqlSession session=sessionFactory.openSession(); Tuser t=new Tuser(); t.setT_id(1); t.setT_name("link"); t.setT_password("123456"); int result=session.insert("insertOneUsers", t); if(result==1){ System.out.println("增加成功"); session.commit(); }else{ System.out.println("增加失败"); } session.close(); } //用户删除; @Test public void deleteUserById() throws IOException { //将配置文件转换为字节流 InputStream is=Resources.getResourceAsStream("configuration.xml"); //创建SqlSessionFactory工厂 SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is); //创建SqlSession(打开了一个会话) SqlSession session=sessionFactory.openSession(); int result=session.delete("deleteOneUsers", 3); System.out.println(result); if(result==1){ System.out.println("删除成功"); session.commit(); }else{ System.out.println("删除失败"); } session.close(); } }
5.User.xml
6.configuration.xml
-->
8.效果如下:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~