select sysdate from dual;
--账户表
--账户编号,账户卡号,账户密码,账户余额,账户状态,创建时间
drop table account;
create table account
(
id number(10) primary key,
account_number varchar2(50) not null,
account_pwd varchar2(10) not null,
account_money number(10,2) not null,
account_status number(5) check(account_status in(0,1)),
creation_time date default sysdate
); --创建序列
drop sequence seq_account;
create sequence seq_account; --添加数据
insert into account(
id,account_number,account_pwd,
account_money,account_status,creation_time
)
values
(seq_account.nextval,'6402211990052633201','123',
10.00,1,to_date('2010-08-09 12:12:12','yyyy-MM-dd hh24:mi:ss')); insert into account(
id,account_number,account_pwd,
account_money,account_status,creation_time
)
values
(seq_account.nextval,'6402211990052633202','123',
20.00,1,to_date('2010-08-10 12:12:12','yyyy-MM-dd hh24:mi:ss')); insert into account(
id,account_number,account_pwd,
account_money,account_status,creation_time
)
values
(seq_account.nextval,'6402211990052633203','123',
30.00,1,to_date('2010-08-11 12:12:12','yyyy-MM-dd hh24:mi:ss')); insert into account(
id,account_number,account_pwd,
account_money,account_status,creation_time
)
values
(seq_account.nextval,'6402211990052633204','123',
40.00,1,to_date('2010-08-12 12:12:12','yyyy-MM-dd hh24:mi:ss')); insert into account(
id,account_number,account_pwd,
account_money,account_status,creation_time
)
values
(seq_account.nextval,'6402211990052633205','123',
50.00,1,to_date('2010-08-13 12:12:12','yyyy-MM-dd hh24:mi:ss')); insert into account(
id,account_number,account_pwd,
account_money,account_status,creation_time
)
values
(seq_account.nextval,'6402211990052633206','123',
60.00,1,to_date('2010-08-14 12:12:12','yyyy-MM-dd hh24:mi:ss')); insert into account(
id,account_number,account_pwd,
account_money,account_status,creation_time
)
values
(seq_account.nextval,'6402211990052633207','123',
70.00,1,to_date('2010-08-15 12:12:12','yyyy-MM-dd hh24:mi:ss')); select * from (select rownum r,a.* from account a where rownum<=6)t where t.r>3 select * from (select rownum r,a.* from account a where rownum<=#{endrow})t where t.r>#{startrow} insert into account(id,account_number,account_pwd,account_money,account_status,creation_time) values(seq_account.nextval,#{account_number},#{account_pwd},#{account_money},#{account_status},#{creation_time}); update account set account_number=#{account_number},account_pwd=#{account_pwd},account_money=#{account_money},account_status=#{account_status},creation_time=#{creation_time} where id=#{id}

  

3.在com.pojo包下创建Account.java类

public class Account {
private Integer id ;
private String account_number ;
private String account_pwd ;
private Double account_money ;
private Integer account_status ;
private Date creation_time ; public Account() {
super();
} public Account(String accountNumber, String accountPwd,
Double accountMoney, Integer accountStatus, Date creationTime) {
account_number = accountNumber;
account_pwd = accountPwd;
account_money = accountMoney;
account_status = accountStatus;
creation_time = creationTime;
} public Account(Integer id, String accountNumber, String accountPwd,
Double accountMoney, Integer accountStatus, Date creationTime) {
super();
this.id = id;
account_number = accountNumber;
account_pwd = accountPwd;
account_money = accountMoney;
account_status = accountStatus;
creation_time = creationTime;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getAccount_number() {
return account_number;
} public void setAccount_number(String accountNumber) {
account_number = accountNumber;
} public String getAccount_pwd() {
return account_pwd;
} public void setAccount_pwd(String accountPwd) {
account_pwd = accountPwd;
} public Double getAccount_money() {
return account_money;
} public void setAccount_money(Double accountMoney) {
account_money = accountMoney;
} public Integer getAccount_status() {
return account_status;
} public void setAccount_status(Integer accountStatus) {
account_status = accountStatus;
} public Date getCreation_time() {
return creation_time;
} public void setCreation_time(Date creationTime) {
creation_time = creationTime;
} @Override
public String toString() {
return "Account [account_money=" + account_money + ", account_number="
+ account_number + ", account_pwd=" + account_pwd
+ ", account_status=" + account_status + ", creation_time="
+ creation_time + ", id=" + id + "]";
} }

  4.在com.page包下创建分页工具类PageUtil.java

public class PageUtil {
private Integer pageno; //当前页
private Integer pagesize; //页面大小
private Integer totalcount; //同条数
private Integer totalpage; //同页数
private Integer startrow; //起始行
private Integer endrow; //结束行 public PageUtil() {
} /**
* 普通分页
* @param pageno
* @param pagesize
* @param totalcount
*/
public PageUtil(Integer pageno, Integer pagesize, Integer totalcount) {
this.pageno = pageno;
this.pagesize = pagesize;
this.startrow=(pageno-1)*pagesize;
this.endrow=pageno*pagesize;
this.totalcount = totalcount;
this.setTotalpage(totalcount, pagesize);
} public Integer getPageno() {
return pageno;
} public void setPageno(Integer pageno) {
this.pageno = pageno;
} public Integer getPagesize() {
return pagesize;
} public void setPagesize(Integer pagesize) {
this.pagesize = pagesize;
} public Integer getTotalcount() {
return totalcount;
} public void setTotalcount(Integer totalcount) {
this.totalcount = totalcount;
} public Integer getTotalpage() {
return totalpage;
}
/**
* 设置总页数
* @param totalcount
* @param pagesize
*/
public void setTotalpage(Integer totalcount,Integer pagesize) { this.totalpage = totalcount%pagesize==0?totalcount/pagesize:totalcount/pagesize+1;
} public Integer getStartrow() {
return startrow;
} public void setStartrow(Integer startrow) {
this.startrow = startrow;
} public Integer getEndrow() {
return endrow;
} public void setEndrow(Integer endrow) {
this.endrow = endrow;
} }

  5.在com.mapper包下创建数据访问层映射接口AccountMapper.java

public interface AccountMapper {
/**
* 1.分页查询
* @param pageutil
* @return
*/
@Select("select * from (select rownum r,a.* from account a where rownum<=#{endrow})t where t.r>#{startrow}")
List<Account> finaAll(PageUtil pageutil);
/**
* 2.根据id查询
* @param id
* @return
*/
@Select("select * from account where id=#{id}")
Account findById(Integer id);
/**\
* 3.保存账户对象
* @param account
* @return
*/
@Insert("insert into account(id,account_number,account_pwd,account_money,account_status,creation_time) values(seq_account.nextval,#{account_number},#{account_pwd},#{account_money},#{account_status},#{creation_time})")
Integer saveAccount(Account account);
/**\
* 4.修改对象
* @param account
* @return
*/
@Update("update account set account_number=#{account_number},account_pwd=#{account_pwd},account_money=#{account_money},account_status=#{account_status},creation_time=#{creation_time} where id=#{id}")
Integer updateAccount(Account account);
/**
* 5.根据id删除
* @param id
* @return
*/
@Delete("delete from account where id=#{id}")
Integer deleteById(Integer id); }

  

6.在src下创建数据库属性文件jdbc.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:orcl
username=****
password=****

  7.在src下创建主配置文件mybatis-config.xml

<configuration>
<properties resource="jdbc.properties"/> <environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments> <mappers>
<mapper class="com.mapper.AccountMapper"/>
</mappers>
</configuration>

  8.在com.util包下创建获取SqlSession工具类MyBatisUtil.java

public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory=null;
static{
try {
Reader reader=Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
System.out.println("解析xml文件或创建工厂出错!");
}
}
/**
* 获取SqlSession对象
* @param isCommit
* @return
*/
public static SqlSession getSqlSession(boolean isCommit){
return sqlSessionFactory.openSession(isCommit);
} }

  9.在com.mapper包下创建数据访问层映射接口实现类AccountMapperImpl.java

public class AccountMapperImpl implements AccountMapper {
private SqlSession sqlSession=MyBatisUtil.getSqlSession(true);
private AccountMapper mapper=sqlSession.getMapper(AccountMapper.class); public Integer deleteById(Integer id) {
return mapper.deleteById(id);
} public List<Account> finaAll(PageUtil pageutil) {
return mapper.finaAll(pageutil);
} public Account findById(Integer id) {
return mapper.findById(id);
} public Integer saveAccount(Account account) {
return mapper.saveAccount(account);
} public Integer updateAccount(Account account) {
return mapper.updateAccount(account);
} public SqlSession getSqlSession() {
return sqlSession;
} public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
} public AccountMapper getMapper() {
return mapper;
} public void setMapper(AccountMapper mapper) {
this.mapper = mapper;
} }

  10.在com.test包下创建测试类Test.java

public class Test {
public static void main(String[] args) {
AccountMapper mapper=new AccountMapperImpl();
System.out.println("---------------1.分页查询前三条:------------------");
List<Account> list=mapper.finaAll(new PageUtil(1, 3, 7));
for (Account account : list) {
System.out.println(account);
} System.out.println("---------------1.根据id查询第5条:------------------");
Account account=mapper.findById(5);
System.out.println(account);
} }

  

MyBatis基于注解----增删改查的更多相关文章

  1. MyBatis -- 对表进行增删改查(基于注解的实现)

    1.MyBatis对数据库表进行增/删/改/查 前一篇使用基于XML的方式实现对数据库的增/删/改/查 以下我们来看怎么使用注解的方式实现对数据库表的增/删/改/查 1.1  首先须要定义映射sql的 ...

  2. MyBatis简单的增删改查以及简单的分页查询实现

    MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...

  3. Mybatis入门之增删改查

    Mybatis入门之增删改查 Mybatis如果操作成功,但是数据库没有更新那就是得添加事务了.(增删改都要添加)----- 浪费了我40多分钟怀疑人生后来去百度... 导入包: 引入配置文件: sq ...

  4. Mybatis实现简单增删改查

    Mybatis的简单应用 学习内容: 需求 环境准备 代码 总结: 学习内容: 需求 使用Mybatis实现简单增删改查(以下是在IDEA中实现的,其他开发工具中,代码一样) jar 包下载:http ...

  5. 基于SSM之Mybatis接口实现增删改查(CRUD)功能

    国庆已过,要安心的学习了. SSM框架以前做过基本的了解,相比于ssh它更为优秀. 现基于JAVA应用程序用Mybatis接口简单的实现CRUD功能: 基本结构: (PS:其实这个就是用的Mapper ...

  6. mybatis中的增删改查操作

    在这一个部分,主要进行增删改查的示例书写. 增删改查可以基于xml的,也可以基于注解的方式. 一:对单条数据的查询 1.目录结构 这个使得目录更加清晰 2.User.java 这个使用以前的user表 ...

  7. SpringMVC,MyBatis商品的增删改查

    一.需求 商品的增删改查 二.工程结构 三.代码 1.Mapper层 (1) ItemsMapperCustom.java package com.tony.ssm.mapper; import ja ...

  8. SpringBoot2+Druid+MyBatis+MySql实现增删改查

    1.配置pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...

  9. Mybatis的简单增删改查

    刚开始学习Mybatis可以先看下官方文档,MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis避免了几乎所有的JDBC代码和手工设置参数以及抽取结果集.MyBat ...

随机推荐

  1. java.net.UnknownHostException异常处理

    1.问题描述 最近迁移环境,在Linux系统下部署Java产品的应用,后台报出如下异常,系统报找不到名为“xxx-houtai1”的主机: 1 java.net.UnknownHostExceptio ...

  2. Apache Phoenix基本操作-1

    本篇我们将介绍phoenix的一些基本操作. 1. 如何使用Phoenix输出Hello World? 1.1 使用sqlline终端命令 sqlline.py SZB-L0023780:2181:/ ...

  3. openstack Neutron分析(3)—— neutron-dhcp-agent源码分析

    1.neutron dhcp3个主要部件分别为什么?2.dhcp模块包含哪些内容?3.Dnsmasq配置文件是如何创建和更新的?4.DHCP agent的信息存放在neutron数据库的哪个表中? 扩 ...

  4. window.name 跨域数据传输

    通过window.name可以实现跨域数据传输. 要解决的功能:  www.a.com/a.html 需要获取到 www.b.com/b.html页面内容的数据 需要3个页面 www.a.com/a. ...

  5. Python列表:元素的修改、添加、删除和排序

    本文参考自<Python编程:从入门到实践>,作者:Eric Matthes,译者:袁国忠 操作 语法 举例 结果 修改元素   motocycles = ['honda', 'yamah ...

  6. Swift中如何使用 #if DEBUG

    Swift暂时还不支持大多数的预处理宏操作,但是可以支持“#if/#else/#endif”语句. 下面进行简单的设置使 #if DEBUG 有效,更详细的内容见:http://stackoverfl ...

  7. GIT 应用gitreview方式提交代码过程

    t status -- 是不是修改的文件 git diff (文件名) -- 看文件修改位置 git add (文件名的空格串) git commit -- 提交到本地 git stash -- 暂存 ...

  8. 倒置字符串函数reverse

    倒置字符串函数reverse:用于倒置字符串s中的各个字符的位置, 如原来字符串中如果初始值为123456,则通过reverse函数可将其倒置为654321, 程序如下: #include<st ...

  9. DSD, DFF, DSF, DST概念解析

    DSD = Direct Stream Digital;  DST = D DSD是技术原理. DSDIFF简称DFF 是文件后缀名 DSF也是文件后缀名,他们是一个意思,只是一个是飞利浦的叫法 一个 ...

  10. OUTlook无法预览xls文件

    outlook可以正常预览doc,pdf,jpg格式的附件,但是xls和xlsx格式就是不能预览.找了好多网络上的办法,都是不行,最终还是找一个靠谱的办法,记录一下 这个方法非常有用:如题, 本人安装 ...