<select id="selAll1" resultMap="mymap1"> select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teacher t LEFT JOIN student s on t.id=s.tid; </select> </mapper>
//只列举了变量 private int id; private String name; private int age; private int tid; private Teacher teacher; //老师类的对象!!!!!!
2. teacher配置文件(一个是student)
1 2 3 4 5 6
<mapper namespace="com.bjsxt.mapper.TeacherMapper"> <select id="selAll" resultType="teacher"> select t.id id,t.name name,s.id `list.id`,s.name `list.name`,age `list.age`,tid `list.tid` from teacher t LEFT JOIN student s on t.id=s.tid //展示的是list集合输出 </select> </mapper>
3. student配置文件(里面通过老师对象(反单引号) teacher.id等传过来用)
1 2 3 4 5 6 7
<mapper namespace="com.bjsxt.mapper.StudentMapper"> <select id="selAll" resultType="student"> select t.id `teacher.id`,t.name `teacher.name`,s.id id,s.name name,age,tid from student s LEFT JOIN teacher t on t.id=s.tid //里面通过老师对象(反单引号) teacher.id等传过来用) </select> </mapper>
public class Test { public static void main(String[] args) { //使用ThreadLocal类 final ThreadLocal<String> threadLocal=new ThreadLocal<>(); //final不让实例化 threadLocal.set("测试"); //创建子线程 new Thread() { public void run() //匿名内部类 { String result = threadLocal.get(); System.out.println("结果"+result); }; }.start(); String result = threadLocal.get(); System.out.println(result); } }
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.AccountMapper"> <!-- 根据帐号和密码查询账户信息 --> <select id="selByAccnoPwd" resultType="account" parameterType="account"> //id就是此方法名称 resultType就是返回值类型 select * from account where accno=#{accNo} and password=#{password} </select>
<!-- 根据帐号和姓名查询账户信息 --> <select id="selByAccnoName" resultType="account" parameterType="account"> select * from account where accno=#{accNo} and name=#{name} </select>
<!-- 根据accNo修改账户余额 --> <update id="updBalanceByAccno" parameterType="account"> update account set balance=balance+#{balance} where accno=#{accNo} </update>
</mapper>
准备serive包下的AccountService接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public interface AccountService { //帐号和密码不匹配状态码 int ACCOUNT_PASSWORD_NOT_MATCH=1; //余额不足 int ACCOUNT_BALANCE_NOT_ENOUGH=2; //账户姓名不匹配 int ACCOUNT_NAME_NOT_MATCH=3; //转账失败 int ERROR=4; //转账成功 int SUCCESS=5; //转账功能 int transfer(Account accIn,Account accOut) throws IOException; }
public class People { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "People [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
public class People { private int id; //和数据库的属性匹配 private String name; private int age; public int getId() { //生成所有的set和get方法 return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }