zoukankan      html  css  js  c++  java
  • mybatis实现一对多连接查询

     

    问题:两个对象User和Score,它们之间的关系为一对多。 

    底层数据库为postgresql,ORM框架为mybatis。 

    关键代码如下: 


    mybatis配置文件如下: 

    mybatis.xml文件内容为: 
    Java代码  收藏代码
    1.  <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE configuration  
    3.     PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
    4.     "http://mybatis.org/dtd/mybatis-3-config.dtd">  
    5. <configuration>  
    6.     <settings>  
    7.         <setting name="cacheEnabled" value="true" />  
    8.         <setting name="lazyLoadingEnabled" value="true" />  
    9.         <setting name="multipleResultSetsEnabled" value="true" />  
    10.         <setting name="useColumnLabel" value="true" />  
    11.         <setting name="useGeneratedKeys" value="true" />  
    12.         <setting name="defaultExecutorType" value="SIMPLE" />  
    13.         <setting name="defaultStatementTimeout" value="25000" />  
    14.     </settings>  
    15.     <typeAliases>  
    16.         <typeAlias type="com.mybatis.domain.User" alias="User" />  
    17.         <typeAlias type="com.mybatis.domain.Score" alias="Score" />  
    18.     </typeAliases>  
    19.     <environments default="development">  
    20.         <environment id="development">  
    21.             <transactionManager type="JDBC" />  
    22.             <dataSource type="POOLED">  
    23.                 <property name="driver" value="org.postgresql.Driver" />  
    24.                 <property name="url" value="jdbc:postgresql://localhost:5432/mybatis" />  
    25.                 <property name="username" value="postgres" />  
    26.                 <property name="password" value="admin" />  
    27.             </dataSource>  
    28.         </environment>  
    29.     </environments>  
    30.   
    31.     <mappers>  
    32.         <mapper resource="com/mybatis/domain/User.xml" />  
    33.         <mapper resource="com/mybatis/domain/Score.xml" />  
    34.     </mappers>  
    35. </configuration>  


    User.java代码为: 
    Java代码  收藏代码
    1. package com.mybatis.domain;  
    2.   
    3. public class User {  
    4.   
    5.     private Integer id;//用户id  
    6.   
    7.     private String username;//用户名  
    8.   
    9.     private String password;//密码  
    10.   
    11.     private String address;//地址  
    12.   
    13.     public User(){  
    14.           
    15.     }  
    16.       
    17.     public User(String username,String password,String address){  
    18.         this.username = username;  
    19.         this.password = password;  
    20.         this.address =address;  
    21.     }  
    22.       
    23.     public User(Integer id,String username,String password,String address){  
    24.         this.id = id;  
    25.         this.username = username;  
    26.         this.password = password;  
    27.         this.address =address;  
    28.     }  
    29.   
    30.     public int getId() {  
    31.         return id;  
    32.     }  
    33.   
    34.     public void setId(Integer id) {  
    35.         this.id = id;  
    36.     }  
    37.   
    38.     public String getUsername() {  
    39.         return username;  
    40.     }  
    41.   
    42.     public void setUsername(String username) {  
    43.         this.username = username;  
    44.     }  
    45.   
    46.     public String getPassword() {  
    47.         return password;  
    48.     }  
    49.   
    50.     public void setPassword(String password) {  
    51.         this.password = password;  
    52.     }  
    53.   
    54.     public String getAddress() {  
    55.         return address;  
    56.     }  
    57.   
    58.     public void setAddress(String address) {  
    59.         this.address = address;  
    60.     }  
    61.       
    62.     public String toString(){  
    63.         return "当前用户为:id = "+id+",username = "+username+",password = "+password+",address = "+address;  
    64.     }  
    65.   
    66. }  


    Score.java代码如下: 
    Java代码  收藏代码
    1.  package com.mybatis.domain;  
    2.   
    3. public class Score {  
    4.       
    5.     private Integer id ;//主键id  
    6.       
    7.     private User user;//所属用户  
    8.       
    9.     private int math ;//数学成绩  
    10.       
    11.     private int chinese ;//语文成绩  
    12.       
    13.     private int english ;//英语成绩  
    14.       
    15.     private int computer ;//计算机成绩  
    16.       
    17.     public Score(){  
    18.           
    19.     }  
    20.       
    21.     public Score(User user, int math,int chinese,int english,int computer){  
    22.         this.user = user;  
    23.         this.math = math;  
    24.         this.chinese = chinese;  
    25.         this.english = english;  
    26.         this.computer = computer;  
    27.     }  
    28.   
    29.     public Integer getId() {  
    30.         return id;  
    31.     }  
    32.   
    33.     public void setId(Integer id) {  
    34.         this.id = id;  
    35.     }  
    36.   
    37.     public User getUser() {  
    38.         return user;  
    39.     }  
    40.   
    41.     public void setUser(User user) {  
    42.         this.user = user;  
    43.     }  
    44.   
    45.     public int getMath() {  
    46.         return math;  
    47.     }  
    48.   
    49.     public void setMath(int math) {  
    50.         this.math = math;  
    51.     }  
    52.   
    53.     public int getChinese() {  
    54.         return chinese;  
    55.     }  
    56.   
    57.     public void setChinese(int chinese) {  
    58.         this.chinese = chinese;  
    59.     }  
    60.   
    61.     public int getEnglish() {  
    62.         return english;  
    63.     }  
    64.   
    65.     public void setEnglish(int english) {  
    66.         this.english = english;  
    67.     }  
    68.   
    69.     public int getComputer() {  
    70.         return computer;  
    71.     }  
    72.   
    73.     public void setComputer(int computer) {  
    74.         this.computer = computer;  
    75.     }  
    76.       
    77.     public String toString(){  
    78.         return "id = "+ this.id+",math = "+this.math+",chinese = "+this.chinese+",english = "+this.english+",computer = "+this.computer+  
    79.                 ", userid = "+this.user.getId()+",username = "+this.user.getUsername()+",password = "+this.user.getPassword()+  
    80.                 ",address = "+this.user.getAddress();  
    81.     }  
    82.   
    83. }  


    user.xml中的关键代码为: 
    Java代码  收藏代码
    1. <resultMap type="User" id="userResult">  
    2.   <id property="id" column="userid"/>  
    3.   <result property="username" column="username"/>  
    4.   <result property="password" column="password"/>  
    5.   <result property="address" column="address"/>  
    6. </resultMap>  

    这里的对象的属性id对应的数据库表列名为userid,这是user对象为pg_score表中 

    的标示。 

    score.xml代码如下: 
    Java代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
    3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
    4.   
    5. <mapper namespace="ScoreDaoMapping">  
    6.     <resultMap type="Score" id="score">  
    7.         <constructor>  
    8.             <idArg column="id" javaType="int" />  
    9.             <arg column="userid" javaType="int" />  
    10.             <arg column="math" javaType="int" />  
    11.             <arg column="chinese" javaType="int" />  
    12.             <arg column="english" javaType="int" />  
    13.             <arg column="computer" javaType="int" />  
    14.         </constructor>  
    15.     </resultMap>  
    16.       
    17.     <resultMap id="joinSelectScore" type="Score" >  
    18.       <id property="id" column="id"/>  
    19.       <result property="math" column="math"/>  
    20.       <result property="chinese" column="chinese"/>  
    21.       <result property="english" column="english"/>  
    22.       <result property="computer" column="computer"/>  
    23.       <association property="user" column="userid" javaType="User" resultMap="UserDaoMapping.userResult"/>  
    24.     </resultMap>  
    25.       
    26.     <insert id="insertScore" parameterType="Score">  
    27.        insert into pg_score(math,chinese,english,computer,userid) values(#{math},#{chinese},#{english},#{computer},#{user.id})  
    28.     </insert>  
    29.       
    30.     <select id="findScoreByUser" resultMap="joinSelectScore" resultType="list" parameterType="map">  
    31.          select   
    32.                 s.id as id,  
    33.                 s.math as math,  
    34.                 s.chinese as chinese,  
    35.                 s.english as english,  
    36.                 s.computer as computer,  
    37.                 u.id as userid,  
    38.                 u.username as username,  
    39.                 u.password as password,  
    40.                 u.address as address  
    41.          from pg_score s left outer join pg_userinfo u on s.userid = u.id where u.id=#{userid}   
    42.     </select>  
    43.      
    44. </mapper>  


    ScoreDao.java中的关键代码为: 
    Java代码  收藏代码
    1. private  String resource = "com/mybatis/configuration/mybatis.xml";  
    2.     public List<Score> selectScoreByUser(User user) throws IOException{  
    3.         Reader reader = Resources.getResourceAsReader(resource);  
    4.         SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);  
    5.         SqlSession session = ssf.openSession();  
    6.         reader.close();  
    7.         Map<String,Integer> params = new HashMap<String,Integer>();  
    8.         params.put("userid", user.getId());  
    9.         List<Score> scoreList = session.selectList("ScoreDaoMapping.findScoreByUser", params);  
    10.         session.commit();  
    11.         session.close();  
    12.         return scoreList;  
    13.     }  


    ScoreService.java代码如下: 
    Java代码  收藏代码
    1. package com.mybatis.service;  
    2.   
    3. import java.io.IOException;  
    4. import java.util.List;  
    5.   
    6. import com.mybatis.dao.ScoreDao;  
    7. import com.mybatis.domain.Score;  
    8. import com.mybatis.domain.User;  
    9.   
    10. public class ScoreService {  
    11.   
    12.     private ScoreDao scoreDao = new ScoreDao();  
    13.   
    14.     public ScoreDao getScoreDao() {  
    15.         return scoreDao;  
    16.     }  
    17.   
    18.     public void setScoreDao(ScoreDao scoreDao) {  
    19.         this.scoreDao = scoreDao;  
    20.     }  
    21.       
    22.     public List<Score> getScoreByUser(User user) throws IOException{  
    23.         return scoreDao.selectScoreByUser(user);  
    24.     }  
    25.       
    26.     public void insertScore(Score score) throws IOException{  
    27.         scoreDao.insert(score);  
    28.     }  
    29. }  


    Test.java代码如下: 

    Java代码  收藏代码
    1. package com.mybatis.test;  
    2.   
    3. import java.util.List;  
    4.   
    5. import com.mybatis.domain.Score;  
    6. import com.mybatis.domain.User;  
    7. import com.mybatis.service.ScoreService;  
    8. import com.mybatis.service.UserService;  
    9.   
    10. public class Test {  
    11.   
    12.     private UserService userService = new UserService();  
    13.       
    14.     private ScoreService scoreSerice = new ScoreService();  
    15.       
    16.     public static void main(String[] args) throws Exception{  
    17.           
    18.         Test test = new Test();  
    19.         //test.insertScore();  
    20.         List<Score> scoreList = test.getScore();  
    21.         Score score = null;  
    22.         for(int i=0;i<scoreList.size();i++){  
    23.             System.out.println("第"+(i+1)+"个score对象为:");  
    24.             score = scoreList.get(i);  
    25.             System.out.println(score);  
    26.         }  
    27.           
    28.     }  
    29.       
    30.     public void insertScore() throws Exception{  
    31.         List<User> userList = userService.getPageUsers(10, 0);  
    32.         User user = userList.get(2);  
    33.           
    34.         Score score = new Score();  
    35.         score.setUser(user);  
    36.         score.setChinese(80);  
    37.         score.setComputer(90);  
    38.         score.setEnglish(91);  
    39.         score.setMath(98);  
    40.         scoreSerice.insertScore(score);  
    41.     }  
    42.       
    43.     public List<Score> getScore() throws Exception{  
    44.         List<User> userList = userService.getPageUsers(10, 0);  
    45.         User user = userList.get(0);  
    46.           
    47.         List<Score> scoreList = scoreSerice.getScoreByUser(user);  
    48.         return scoreList;  
    49.     }  
    50.       
    51. /*  public User getUserById(int id) throws Exception{ 
    52.         return userService.getUserById(id); 
    53.     }*/  
    54. }
  • 相关阅读:
    Bower 使用
    为什么是static?
    多重继承 -Javascript中的apply与call详解
    留用 未验证 js适配根字体大小
    Js作用域与作用域链详解
    理解AngularJS中的依赖注入
    渐进增强 优雅降级
    前后台数据交换的几种方式:
    then()方法是异步执行
    HTML怎么让img 等比例缩放
  • 原文地址:https://www.cnblogs.com/zengda/p/5118819.html
Copyright © 2011-2022 走看看