config.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 4 5 <configuration> 6 7 <!-- 次属性可以指定类的全路径,同时给他一个别名 相当于(id="'class="") 8 <typeAliases > 9 <typeAlias type="com.mybatis.entry.userMapper" alias="umapper"/> 10 </typeAliases> 11 --> 12 13 <!-- 默认创建此工厂 默认模式 --> 14 <environments default="development"> 15 <environment id="development"> 16 <transactionManager type="JDBC" /> 17 <!-- 配置数据库连接信息 --> 18 <dataSource type="POOLED"> 19 <property name="driver" value="com.mysql.jdbc.Driver" /> 20 <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> 21 <property name="username" value="root" /> 22 <property name="password" value="root" /> 23 </dataSource> 24 </environment> 25 </environments> 26 27 28 <mappers> 29 <!-- 注册userMapper.xml文件, 30 userMapper.xml位于me.gacl.mapping这个包下, 31 所以resource写成me/gacl/mapping/userMapper.xml 32 --> 33 <mapper resource="com/mybatis/dao/userMapper.xml"/> 34 </mappers> 35 </configuration>
UserMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 4 <!-- namespace命名空间,作用就是对sql进行分类化的管理,理解为sql隔离 5 注意:使用mapper代理开发时,namespace有特殊作用 6 --> 7 8 <!-- <mapper namespace = "umapper"> --> 9 <mapper namespace = "com.mybatis.dao.UserMapper"> 10 <!-- 通过SELECT执行数据库查询 11 id:标识映射文件中的sql,称为statement的id; 12 将sql语句封装在mapperStatement的对象中,所以Id称为Statement的id; 13 parameterType:指定输入参数的类型,这里指定int型 14 #{}:表示一个占位符; 15 #{id}:其中Id表示接收输入的参数,参数名称就是Id,如果输入参数是简单类型,#{}中的参数名可以任意,可以是value或者其它名称; 16 resultType:指定sql输出结果所映射的java对象类型,select指定resultType表示将单条记录映射成java对象。 17 --> 18 19 <select id = "selectById" parameterType="int" resultType="com.mybatis.entry.User"> 20 select * from user where id=#{id} 21 </select> 22 <!-- 根据用户名称模糊查询用户信息,可能返回多条数据 23 resultType:指定的就是单条记录所映射的java类型; 24 ${}:表示拼接sql字符串,将接收到的参数内容不加任何修饰拼接在sql中. 25 使用${}拼接sql,可能会引起sql注入 26 ${value}:接收输入参数的内容,如果传入的是简单类型,${}中只能使用value 27 --> 28 29 <select id="selectUserAndYear" resultType="com.mybatis.entry.UserMore"> 30 select `user`.*,phone.* 31 from user,phone 32 where phone.phonenum = `user`.phoneNum and name=#{name}; 33 </select> 34 35 <!-- 数据库字段与类属性名不一致时 --> 36 <select id="selectUserAndYearMap" resultMap="UserMap" > 37 select `user`.*,phone.* 38 from user,phone 39 where phone.phonenum = `user`.phoneNum and name=#{name}; 40 </select> 41 <!-- 一对一的resultMap关联方式 --> 42 <resultMap type="com.mybatis.entry.User" id="UserMap"> 43 <id column="id" property="id"/> 44 <result column="name" property="name"/> 45 <result column="phoneNum" property="phoneNum"/> 46 47 <association property="phone" javaType="com.mybatis.entry.Phone"> 48 <id column="id" property="id"/> 49 <result column="phoneNum" property="phoneNum"/> 50 <result column="year" property="year"/> 51 </association> 52 </resultMap> 53 54 55 <select id="selectUserAndQQMap" resultMap="QQMap"> 56 select `user`.*,qq.* 57 from user,qq 58 where `user`.id = qq.userId and user.name=#{name}; 59 </select> 60 <!-- 一对多的resultMap关联方式 --> 61 <resultMap type="com.mybatis.entry.User" id="QQMap"> 62 <id column="id" property="id"/> 63 <result column="name" property="name"/> 64 <result column="phoneNum" property="phoneNum"/> 65 66 <collection property="qqs" ofType="com.mybatis.entry.QQ"> 67 <id column="qq_id" property="id"/> 68 <result column="qqName" property="qqName"/> 69 <result column="password" property="password"/> 70 <result column="userId" property="userId"/> 71 </collection> 72 </resultMap> 73 </mapper>
UserMapper.java
1 package com.mybatis.dao; 2 3 import java.util.List; 4 5 import com.mybatis.entry.User; 6 import com.mybatis.entry.UserMore; 7 8 public interface UserMapper { 9 10 public User selectById(int id)throws Exception; 11 12 public UserMore selectUserAndYear(String name)throws Exception; 13 14 public List<User> selectUserAndYearMap(String name)throws Exception; 15 16 public List<User> selectUserAndQQMap(String name)throws Exception; 17 18 19 }
UserService.java
1 package com.mybatis.service; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.List; 6 7 import org.apache.ibatis.io.Resources; 8 import org.apache.ibatis.session.SqlSession; 9 import org.apache.ibatis.session.SqlSessionFactory; 10 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 12 import com.mybatis.dao.UserMapper; 13 import com.mybatis.entry.User; 14 import com.mybatis.entry.UserMore; 15 16 public class UserService { 17 18 SqlSessionFactory factory; 19 20 public void before() throws IOException{ 21 InputStream in = Resources.getResourceAsStream("conf.xml"); 22 factory = new SqlSessionFactoryBuilder().build(in); 23 } 24 25 public User selectById(int id) throws Exception{ 26 //先获取一个session工厂 27 before(); 28 //生产出一个session 29 SqlSession session = factory.openSession(); 30 //获取user session对象(代理人) 31 UserMapper mapper = session.getMapper(UserMapper.class); 32 33 User user = mapper.selectById(id); 34 return user; 35 } 36 37 public UserMore selectUserAndYear(String name) throws Exception{ 38 before(); 39 SqlSession session = factory.openSession(); 40 UserMapper mapper = session.getMapper(UserMapper.class); 41 UserMore more = mapper.selectUserAndYear(name); 42 return more; 43 } 44 45 public List<User> selectUserAndYearMap(String name) throws Exception{ 46 before(); 47 SqlSession session = factory.openSession(); 48 UserMapper mapper = session.getMapper(UserMapper.class); 49 List<User> list = mapper.selectUserAndYearMap(name); 50 return list; 51 } 52 53 public List<User> selectUserAndQQMap(String name) throws Exception{ 54 before(); 55 SqlSession session = factory.openSession(); 56 UserMapper mapper = session.getMapper(UserMapper.class); 57 List<User> list = mapper.selectUserAndQQMap(name); 58 return list; 59 } 60 }
User.java
1 package com.mybatis.entry; 2 3 import java.util.List; 4 5 public class User { 6 7 private Integer id; 8 private String name; 9 private String phoneNum; 10 private Phone phone; 11 private List<QQ> qqs; 12 13 public User() { 14 super(); 15 } 16 17 public User(String name, String phoneNum) { 18 this.name = name; 19 this.phoneNum = phoneNum; 20 } 21 22 public User(Integer id, String name, String phoneNum) { 23 super(); 24 this.id = id; 25 this.name = name; 26 this.phoneNum = phoneNum; 27 } 28 29 30 31 public List<QQ> getQqs() { 32 return qqs; 33 } 34 35 public void setQqs(List<QQ> qqs) { 36 this.qqs = qqs; 37 } 38 39 public Phone getPhone() { 40 return phone; 41 } 42 43 public void setPhone(Phone phone) { 44 this.phone = phone; 45 } 46 47 public Integer getId() { 48 return id; 49 } 50 public void setId(Integer id) { 51 this.id = id; 52 } 53 public String getName() { 54 return name; 55 } 56 public void setName(String name) { 57 this.name = name; 58 } 59 public String getPhoneNum() { 60 return phoneNum; 61 } 62 public void setPhoneNum(String phoneNum) { 63 this.phoneNum = phoneNum; 64 } 65 66 @Override 67 public String toString() { 68 return "User [id=" + id + ", name=" + name + ", phoneNum=" + phoneNum + ", phone=" + phone + ", qqs=" + qqs 69 + "]"; 70 } 71 72 73 74 75 }
QQ.java
1 package com.mybatis.entry; 2 3 public class QQ { 4 private Integer id; 5 private String qqName; 6 private String password; 7 private Integer userId; 8 9 10 public QQ() { 11 super(); 12 } 13 public Integer getId() { 14 return id; 15 } 16 public void setId(Integer id) { 17 this.id = id; 18 } 19 public String getQqName() { 20 return qqName; 21 } 22 public void setQqName(String qqName) { 23 this.qqName = qqName; 24 } 25 public String getPassword() { 26 return password; 27 } 28 public void setPassword(String password) { 29 this.password = password; 30 } 31 public Integer getUserId() { 32 return userId; 33 } 34 public void setUserId(Integer userId) { 35 this.userId = userId; 36 } 37 @Override 38 public String toString() { 39 return "QQ [id=" + id + ", qqName=" + qqName + ", password=" + password + ", userId=" + userId + "]"; 40 } 41 42 }
Phone.java
1 package com.mybatis.entry; 2 3 public class Phone { 4 5 private Integer id; 6 private String phoneNum; 7 private Integer year; 8 9 public Integer getId() { 10 return id; 11 } 12 public void setId(Integer id) { 13 this.id = id; 14 } 15 public String getPhoneNum() { 16 return phoneNum; 17 } 18 public void setPhoneNum(String phoneNum) { 19 this.phoneNum = phoneNum; 20 } 21 public Integer getYear() { 22 return year; 23 } 24 public void setYear(Integer year) { 25 this.year = year; 26 } 27 28 @Override 29 public String toString() { 30 return "Phone [id=" + id + ", phoneNum=" + phoneNum + ", year=" + year + "]"; 31 } 32 33 34 }
UserMore.java
1 package com.mybatis.entry; 2 3 public class UserMore extends User{ 4 5 private Integer year; 6 7 public UserMore() { 8 super(); 9 } 10 11 public Integer getYear() { 12 return year; 13 } 14 15 public void setYear(Integer year) { 16 this.year = year; 17 } 18 19 @Override 20 public String toString() { 21 return "UserMore [year=" + year + "]"; 22 } 23 24 25 }
Test.java
1 package com.mybatis.test; 2 3 import java.util.List; 4 5 import com.mybatis.entry.QQ; 6 import com.mybatis.entry.User; 7 import com.mybatis.entry.UserMore; 8 import com.mybatis.service.UserService; 9 10 public class Test { 11 12 @org.junit.Test 13 public void testSelectById() throws Exception{ 14 UserService service = new UserService(); 15 User user= service.selectById(1); 16 System.out.println(user); 17 } 18 19 @org.junit.Test 20 public void testUserAndYear() throws Exception{ 21 UserService service = new UserService(); 22 UserMore more= service.selectUserAndYear("刘杨"); 23 System.out.println("id:"+more.getId()+" name:"+more.getName()+ 24 " phoneNum:"+more.getPhoneNum()+" year:"+more.getYear()); 25 } 26 27 @org.junit.Test 28 public void testUserAndYearMap() throws Exception{ 29 UserService service = new UserService(); 30 List<User> list = service.selectUserAndYearMap("刘杨"); 31 for(User u:list){ 32 System.out.println(u.getId()+" "+u.getName()+" "+u.getPhoneNum()+" "+u.getPhone().getYear()); 33 System.out.println(u); 34 } 35 } 36 37 @org.junit.Test 38 public void testUserAndQQMap() throws Exception{ 39 UserService service = new UserService(); 40 List<User> list = service.selectUserAndQQMap("刘杨"); 41 for(User u:list){ 42 System.out.print(u.getName()+ "=: "); 43 System.out.println(u); 44 for(QQ qq:u.getQqs()){ 45 System.out.println(qq); 46 } 47 } 48 } 49 50 51 52 }