01_根据Id查询User的数据
【工程目录】
【数据库表中内容 user表】
【sqlMapConfig.xml配置文件主要内容】
简述:sqlMapConfig.xml配置文件主要有两个作用:
1.配置和数据连接的相关信息,例如事务管理、数据库连接池等信息。
2.加载映射文件,例如本项目中的user.xml
<?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> <!-- 和Spring整合之后这些配置将删除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/mybatisdb?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments> <!-- 加载映射文件 -->
<mappers>
<mapper resource="sqlmap/user.xml"/>
</mappers> </configuration>
【user.xml】
在user.xml文件中编写相关的CRUD的代码
<?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="test">
<select id="findUserById" parameterType="int" resultType="com.Higgin.Mybatis.po.User">
SELECT * from USER WHERE id=#{id}
</select>
</mapper>
解释:
1.namespace命名空间,作用就是对sql进行分类化管理。注意:使用mapper代理方法开发,namespace有特殊重要的作用。
2.在<mapper>映射文件中可以配置很多的sql语句,除了<select>还有<delete>、<update>、<insert>等等
3.通过select 执行数据库查询,将sql语句封装到mapperStatement对象中,所以将id成为statement的id
4.parameterType:指定输入参数的类型,这里指定是int类型
5.#{}:表示一个占位符
6.#{id}:其中的占位符表示接入输入的参数,参数名称为id,若输入的参数是简单类型,#{}中的参数名称可以任意,既可以是value或者其他名称
7.resultType:指定sql输出结果所映射的java类型对象,select指定resultType表示将单条记录映射成的User对象
【log4j.properties】
#Global logging configuration
#在开发环境下,日志级别要设置成DEBUG
log4j.rootLogger=DEBUG,stdout
#Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
【User.java】
package com.Higgin.Mybatis.po; import java.util.Date; public class User {
//属性名和数据库表字段对应
private int id;
private String username;
private String sex;
private Date birthday;
private String address;
//忽略get、set、toString方法....
}
【MyBatisTest.java】
public class MyBatisTest {
@Test
public void testFindUserByID() throws IOException{
//mybatis配置文件
String resource="SqlMapConfig.xml"; //得到配置文件
InputStream inputStream=Resources.getResourceAsStream(resource); //创建会话工厂,传入mybatis的配置文件信息
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); //通过工厂得到SqlSession
SqlSession sqlSession=sqlSessionFactory.openSession(); //通过SqlSession操作数据库
//第一个参数:映射文件中statement的id,等于:namespace+"."+statement的id
//第二个参数:指定映射文件中的所匹配的parameterType类型的参数
User user=sqlSession.selectOne("test.findUserById", 2);
System.out.println(user.toString());
//释放资源
sqlSession.close();
}
}
【运行结果】
01_根据Id查询User的数据的更多相关文章
- MySQL 中根据A表的ID查询B表数据
例1:查询某个文章及其对应的评论(单个详情) ) FROM A; 例2:查询分类表中,每种分类各包含多少商品(汇总) SELECT category_id, (SELECT count(goods_i ...
- yii根据id查询一条数据
model层 public function selectone($ag_id=''){ return $this->findBySql("SELECT * FROM agency w ...
- mongoose实现批量删除和多id查询的api/方法
删除一条数据:传入id Model.remove({ _id: 传入的id }); 删除多条数据,传入id数组,使用$in方法 Model.remove({ _id: { $in: ['aID', ' ...
- 根据id查询数据(向前台返回json格式的数据)
/** *@description 根据主键查询Bean */ @RequestMapping(value="/getBean/{getId}") public void getB ...
- sql查询删除重复数据
数据库UserInfo 删除重复数据 即删除重复的用户名手机号 同一个用户名手机号只保留一个用户 01.根据多个字段查询重复数据 with data1 as( select MobilePhone,N ...
- 使用PreparedStatement 查询一条数据 封装成一个学生的Student1对象
package cn.lijun.entity; public class Student1 { private int id; private String sname; private int g ...
- Oracle数据库--解决单张表中数据量巨大(大数据、数据量上百万级别,后查询,更新数据等耗时剧增)
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/QQ578473688/article/details/54561397 思路1:采用备份表 备份表中 ...
- SQL 中 not in 查询不到数据问题
在开发的过程中,遇到过not in 始终查询不到数据问题 select * from T_CustomerInfo where CustomerID not in (select CustomerID ...
- Struts2自定义标签2自定义一个按班级id查询出该班级下的学生,存放进值栈,并遍历出来。
Struts2自定义标签的流程概念: (1)需要两个类:标签类(继承相应的tag类),基本类(继承Component).标签类专门负责从客户端取得用户输入的一些属性,这个普通的jsp自定义标签一样,取 ...
随机推荐
- Azure 虚拟机常见问题-上
在 Azure 虚拟机上可以运行什么? 所有订户均可在 Azure 虚拟机上运行服务器软件.此外,MSDN 订户还可以访问由 Azure 提供的特定 Windows 客户端映像. 就服务器软件来说,你 ...
- MYSQL存储过程中的IN、OUT和INOUT
MYSQL存储过程中的IN.OUT和INOUT,不能简单理解为一个方法的参数和返回值,而是面向整个过程上下文变量的. 一.MySQL 存储过程参数(in) 基本可以理解为传入function的参数,而 ...
- Storm系列(六)架构分析之Scheduler-调度器[EventScheduler]
任务调度接口定义: 1 IScheduler{ 2 // conf为当前nimbus的stormp配置 3 void prepare(Map conf); // 初始化 4 // to ...
- Add And Reset a Federation Server to a Federation Server Farm adfs ad
Applies To: Active Directory Federation Services (AD FS) 2.0 After you install the Active Directory ...
- Java 线程池详细介绍
根据摩尔定律(Moore’s law),集成电路晶体管的数量差不多每两年就会翻一倍.但是晶体管数量指数级的增长不一定会导致 CPU 性能的指数级增长.处理器制造商花了很多年来提高时钟频率和指令并行.在 ...
- Web Service学习文旦下载
Web Service的学习暂且告一段落,因为毕竟只是对它作简要了解,至于其原理什么并不打算涉及. 在这里我提供下我之前文档的整理版本: http://kuai.xunlei.com/d/YlzvAG ...
- hdu4462 Scaring the Birds
Scaring the Birds Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- java发送带附件的邮件
/** * java发送带附件的邮件 * 周枫 * 2013.8.10 */ package com.dsideal.Util; import javax.mail.*; import javax.m ...
- [TypeScript] 1. Catching JavaScript Mistakes with TypeScript
The TypeScript compiler is a powerful tool which catches mistakes even in vanilla JavaScript. Try it ...
- nginx-systemtap-toolkit
https://github.com/openresty/nginx-systemtap-toolkit