Mybatis入门程序

网友投稿 613 2022-10-08

Mybatis入门程序

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

insert into t_user values(#{t_id},#{t_name},#{t_password}) delete from t_user where t_id=#{id}

6.configuration.xml

7.log4j.propreties# Properties file for log4j configuration # # Place this file in the root CLASSPATH # log4j.rootCategory=ERROR,stdout,logfile #log4j.rootCategory=ERROR,stdout,logfile,Rlogfile log4j.logger.org.springframework=ERROR log4j.logger.org.apache.cxf=ERROR log4j.org.apache.velocity=ERROR log4j.logger.org.powerise.bl3ee=ERROR #bl3ee MVC log log4j.logger.org.powerise.bl3ee.core.mvc.xstruts=ERROR #bl3ee DAO log log4j.logger.org.powerise.bl3ee.core.model.dao=DEBUG #bl3ee ORM log log4j.logger.org.powerise.bl3ee.core.orm.xibatis=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG #bl3ee UI Tag Library log log4j.logger.org.powerise.bl3ee.core.tplengine=ERROR log4j.logger.org.powerise.bl3ee.core.web.taglib.util.TagHelper=ERROR #request monitor log log4j.logger.org.powerise.bl3ee.core.web.RequestFilter=ERROR #jsON encode log log4j.logger.org.powerise.bl3ee.core.json=ERROR # Resource manager log log4j.logger.org.powerise.bl3ee.core.resource=ERROR # (!!!)Please do not modify the 4 configurations log4j.logger.org.powerise.bl3ee.core.web.SystemInitListener=INFO log4j.logger.org.powerise.bl3ee.core.server.BL3Server=INFO log4j.logger.org.powerise.bl3ee.core.model.SpringBeanLoader=INFO log4j.logger.org.powerise.bl3ee.core.model.SpringBeanAspect=ERROR #stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %5p [%t] - %m%n #logfile log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender log4j.appender.logfile.DatePattern=yyyy-MM-dd log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n log4j.appender.logfile.File=c:/BL3Logs/bl3ee.log #Rlogfile log4j.appender.Rlogfile=org.apache.log4j.RollingFileAppender log4j.appender.Rlogfile.layout=org.apache.log4j.PatternLayout log4j.appender.Rlogfile.layout.ConversionPattern=%d %p [%c] - <%m>%n log4j.appender.Rlogfile.File=c:/BL3Logs/bl3ee.log log4j.appender.Rlogfile.MaxFileSize=2048KB log4j.appender.Rlogfile.MaxBackupIndex=100

8.效果如下:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring.NET- .NET应用程序框架(springnet搭建)
下一篇:WordPress 版微信小程序- 利用WordPress rest api 迅速架构微信小程序
相关文章

 发表评论

暂时没有评论,来抢沙发吧~