config.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> <!-- 次属性可以指定类的全路径,同时给他一个别名 相当于(id="'class="")
<typeAliases >
<typeAlias type="com.mybatis.entry.userMapper" alias="umapper"/>
</typeAliases>
--> <!-- 默认创建此工厂 默认模式 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments> <mappers>
<!-- 注册userMapper.xml文件,
userMapper.xml位于me.gacl.mapping这个包下,
所以resource写成me/gacl/mapping/userMapper.xml
-->
<mapper resource="com/mybatis/dao/userMapper.xml"/>
</mappers>
</configuration>

UserMapper.xml

 <?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"> <!-- namespace命名空间,作用就是对sql进行分类化的管理,理解为sql隔离
注意:使用mapper代理开发时,namespace有特殊作用
--> <!-- <mapper namespace = "umapper"> -->
<mapper namespace = "com.mybatis.dao.UserMapper">
<!-- 通过SELECT执行数据库查询
id:标识映射文件中的sql,称为statement的id;
将sql语句封装在mapperStatement的对象中,所以Id称为Statement的id;
parameterType:指定输入参数的类型,这里指定int型
#{}:表示一个占位符;
#{id}:其中Id表示接收输入的参数,参数名称就是Id,如果输入参数是简单类型,#{}中的参数名可以任意,可以是value或者其它名称;
resultType:指定sql输出结果所映射的java对象类型,select指定resultType表示将单条记录映射成java对象。
--> <select id = "selectById" parameterType="int" resultType="com.mybatis.entry.User">
select * from user where id=#{id}
</select>
<!-- 根据用户名称模糊查询用户信息,可能返回多条数据
resultType:指定的就是单条记录所映射的java类型;
${}:表示拼接sql字符串,将接收到的参数内容不加任何修饰拼接在sql中.
使用${}拼接sql,可能会引起sql注入
${value}:接收输入参数的内容,如果传入的是简单类型,${}中只能使用value
--> <select id="selectUserAndYear" resultType="com.mybatis.entry.UserMore">
select `user`.*,phone.*
from user,phone
where phone.phonenum = `user`.phoneNum and name=#{name};
</select> <!-- 数据库字段与类属性名不一致时 -->
<select id="selectUserAndYearMap" resultMap="UserMap" >
select `user`.*,phone.*
from user,phone
where phone.phonenum = `user`.phoneNum and name=#{name};
</select>
<!-- 一对一的resultMap关联方式 -->
<resultMap type="com.mybatis.entry.User" id="UserMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="phoneNum" property="phoneNum"/> <association property="phone" javaType="com.mybatis.entry.Phone">
<id column="id" property="id"/>
<result column="phoneNum" property="phoneNum"/>
<result column="year" property="year"/>
</association>
</resultMap> <select id="selectUserAndQQMap" resultMap="QQMap">
select `user`.*,qq.*
from user,qq
where `user`.id = qq.userId and user.name=#{name};
</select>
<!-- 一对多的resultMap关联方式 -->
<resultMap type="com.mybatis.entry.User" id="QQMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="phoneNum" property="phoneNum"/> <collection property="qqs" ofType="com.mybatis.entry.QQ">
<id column="qq_id" property="id"/>
<result column="qqName" property="qqName"/>
<result column="password" property="password"/>
<result column="userId" property="userId"/>
</collection>
</resultMap>
</mapper>

UserMapper.java

 package com.mybatis.dao;

 import java.util.List;

 import com.mybatis.entry.User;
import com.mybatis.entry.UserMore; public interface UserMapper { public User selectById(int id)throws Exception; public UserMore selectUserAndYear(String name)throws Exception; public List<User> selectUserAndYearMap(String name)throws Exception; public List<User> selectUserAndQQMap(String name)throws Exception; }

UserService.java

 package com.mybatis.service;

 import java.io.IOException;
import java.io.InputStream;
import java.util.List; 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 com.mybatis.dao.UserMapper;
import com.mybatis.entry.User;
import com.mybatis.entry.UserMore; public class UserService { SqlSessionFactory factory; public void before() throws IOException{
InputStream in = Resources.getResourceAsStream("conf.xml");
factory = new SqlSessionFactoryBuilder().build(in);
} public User selectById(int id) throws Exception{
//先获取一个session工厂
before();
//生产出一个session
SqlSession session = factory.openSession();
//获取user session对象(代理人)
UserMapper mapper = session.getMapper(UserMapper.class); User user = mapper.selectById(id);
return user;
} public UserMore selectUserAndYear(String name) throws Exception{
before();
SqlSession session = factory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
UserMore more = mapper.selectUserAndYear(name);
return more;
} public List<User> selectUserAndYearMap(String name) throws Exception{
before();
SqlSession session = factory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> list = mapper.selectUserAndYearMap(name);
return list;
} public List<User> selectUserAndQQMap(String name) throws Exception{
before();
SqlSession session = factory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> list = mapper.selectUserAndQQMap(name);
return list;
}
}

User.java

 package com.mybatis.entry;

 import java.util.List;

 public class User {

     private Integer id;
private String name;
private String phoneNum;
private Phone phone;
private List<QQ> qqs; public User() {
super();
} public User(String name, String phoneNum) {
this.name = name;
this.phoneNum = phoneNum;
} public User(Integer id, String name, String phoneNum) {
super();
this.id = id;
this.name = name;
this.phoneNum = phoneNum;
} public List<QQ> getQqs() {
return qqs;
} public void setQqs(List<QQ> qqs) {
this.qqs = qqs;
} public Phone getPhone() {
return phone;
} public void setPhone(Phone phone) {
this.phone = phone;
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
} @Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", phoneNum=" + phoneNum + ", phone=" + phone + ", qqs=" + qqs
+ "]";
} }

QQ.java

 package com.mybatis.entry;

 public class QQ {
private Integer id;
private String qqName;
private String password;
private Integer userId; public QQ() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getQqName() {
return qqName;
}
public void setQqName(String qqName) {
this.qqName = qqName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
@Override
public String toString() {
return "QQ [id=" + id + ", qqName=" + qqName + ", password=" + password + ", userId=" + userId + "]";
} }

Phone.java

 package com.mybatis.entry;

 public class Phone {

     private Integer id;
private String phoneNum;
private Integer year; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
} @Override
public String toString() {
return "Phone [id=" + id + ", phoneNum=" + phoneNum + ", year=" + year + "]";
} }

UserMore.java

 package com.mybatis.entry;

 public class UserMore extends User{

     private Integer year;

     public UserMore() {
super();
} public Integer getYear() {
return year;
} public void setYear(Integer year) {
this.year = year;
} @Override
public String toString() {
return "UserMore [year=" + year + "]";
} }

Test.java

 package com.mybatis.test;

 import java.util.List;

 import com.mybatis.entry.QQ;
import com.mybatis.entry.User;
import com.mybatis.entry.UserMore;
import com.mybatis.service.UserService; public class Test { @org.junit.Test
public void testSelectById() throws Exception{
UserService service = new UserService();
User user= service.selectById(1);
System.out.println(user);
} @org.junit.Test
public void testUserAndYear() throws Exception{
UserService service = new UserService();
UserMore more= service.selectUserAndYear("刘杨");
System.out.println("id:"+more.getId()+" name:"+more.getName()+
" phoneNum:"+more.getPhoneNum()+" year:"+more.getYear());
} @org.junit.Test
public void testUserAndYearMap() throws Exception{
UserService service = new UserService();
List<User> list = service.selectUserAndYearMap("刘杨");
for(User u:list){
System.out.println(u.getId()+" "+u.getName()+" "+u.getPhoneNum()+" "+u.getPhone().getYear());
System.out.println(u);
}
} @org.junit.Test
public void testUserAndQQMap() throws Exception{
UserService service = new UserService();
List<User> list = service.selectUserAndQQMap("刘杨");
for(User u:list){
System.out.print(u.getName()+ "=: ");
System.out.println(u);
for(QQ qq:u.getQqs()){
System.out.println(qq);
}
}
} }

mybatisProxy的更多相关文章

  1. Mybatis 和 Solon 勾搭在一起,也是个漂亮组合

    故事相关的源码 https://gitee.com/noear/solon_demo/tree/master/demo08.solon_mybatis 故事开讲 Solon 是Java世界里一个新的极 ...

随机推荐

  1. 创建AIX克隆盘

    1.AIX的克隆盘技术 AIX克隆盘,AIX rootvg的备用替换盘,可以用于保留AIX的原始状态,使AIX在进行升级操作时保留一个AIX操作系统的原始映像,在系统需要时实现即时还原,回到升级操作前 ...

  2. libpng Cximage图片处理

    跨平台 开源 png图片处理 https://www.cnblogs.com/lidabo/p/6923426.html Cximage BIPro

  3. 分布式远程获取配置文件config属性(修正)

    1.采用springcloud 获取github上config配置文件属性需要注意的问题; 2.此时github下面的配置文件,命名要特别注意,否则会获取不到配置里面的属性而报错,报错是因为配置文件命 ...

  4. squid之------安装与基本配置

    1.rpm安装squid yum -y install squid 2.squid主要组成部分 服务名:squid 主程序:/usr/sbin/squid 配置目录:/etc/squid 主配置文件: ...

  5. 【NOIP2014模拟10.25A组】画矩形

    题目 分析 由于要求按时间顺序来操作,考虑整体二分: 对于一段二分出来的区间,将左区间的修改和右区间的查询取出来,每次更新每个查询的答案,正确性显然. 现在有一对修改和查询的操作(保证所有的查询都在修 ...

  6. MTBF测试

    本文转载自:https://blog.csdn.net/liuhaoemail/article/details/50531489 MTBF测试 目前,终端侧的可靠性测试基本上是采用称为”MTBF测试” ...

  7. 【java工具类】生成二维码

    /** * 生成二维码图片 * @param text 扫描二维码后跳转的url * @param width 图片宽度 * @param height 图片高度 * @param filePath ...

  8. _vimrc

    set nocompatible source $VIMRUNTIME/vimrc_example.vim source $VIMRUNTIME/mswin.vim behave mswin set ...

  9. 【canvas学习笔记四】绘制文字

    本节我们来学习如何绘制文字. 绘制文字有两个主要的方法: fillText(text, x, y [, maxWidth]) 在x, y位置填充文字text,有一个可选参数maxWidth设置最大绘制 ...

  10. SQL语法——Join详解

    一.INNER JOIN 用法: select column_name(s) from table 1 INNER JOIN table 2 ON table 1.column_name=table ...