首先 创建好项目 然后导入依赖:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> </dependency>
然后,再加入一些配置信息,加载java文件夹下下面的xml文件:
<build> <finalName>g160828_mybatis01</finalName> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build
一:一对一
1.先说一下 一对一的关系:
我的Person类与Card类:
Person:
private String pname; private int pid; private int page; private Card card; //一对一
Card类:
private int uid; private String cnumber;
然后,再是我的dao接口一个方法:
public Person getPersonandCard(int pid);
2.然后,在生成person.xml,这里我就把card.xml浓缩在person.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"> <mapper namespace="com.desert.Dao.IPersonDao"> <resultMap id="myPersonMap" type="com.desert.entity.Person"> <id property="pid" column="pid"></id> <result property="pname" column="pname"></result> <result property="page" column="page"></result> <association property="card" javaType="com.desert.entity.Card"> <id property="uid" column="uid"></id> <result property="cnumber" column="cnumber"></result> </association> </resultMap>
<!--我这里就做一个示列,根据id得到人和卡--> <select id="getPersonandCard" resultMap="myPersonMap" parameterType="int"> select p.*,c.cnumber from person p,card c where p.pid=c.uid and p.pid=#{pid} </select> </mapper>
3.再是配置我的mybatis-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> <!--设置别名--> <typeAliases> <!--<typeAlias type="com.desert.Dto.MyPerson" alias="a"></typeAlias>--> <package name="com.desert"></package> </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/test"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/desert/entity/person.xml"/> </mappers> </configuration>
4.测试:
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config")); //通过sessionFactory获得SqlSession SqlSession sqlSession=sqlSessionFactory.openSession();
IPersonDao iPersonDao=sqlSession.getMapper(IPersonDao.class);
//根据id得到Person和它的Card Person person=iPersonDao.getPersonandCard(1); System.out.println(person+","+person.getCard());
sqlSession.close();
二:一对多:
一对多 ,我就拿一个省份和城市这个关系来说吧:
首先:是我的省份表:
Provinces:
private int pid; private String pname; private Set<Citys> citysSet;然后,再是我的城市表:
City:
private int cid; private String cname; private int pid;
然后,再是我的dao接口一个方法:
public Provinces getProvinces(int pid);
然后,再是我的person.xml文件关于多对多的关系:
<resultMap id="myProvincesMap" type="com.desert.entity.Provinces"> <id property="pid" column="pid"></id> <result property="pname" column="pname"></result> <collection property="citysSet" ofType="com.desert.entity.Citys"> <id property="cid" column="cid"></id> <result property="cname" column="cname"></result> <result property="pid" column="pid"></result> </collection> </resultMap>
<!--我这里就是根据id得到一个省份,然后再点出这个省份下面的城市-->
<select id="getProvinces" resultMap="myProvincesMap" parameterType="int"> select p.*,c.cname from provinces p,citys c where p.pid=c.pid and p.pid=#{pid} </select>
然后,再去测试就好了:
try {
//加载配置文件 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config")); //通过sessionFactory获得SqlSession SqlSession sqlSession=sqlSessionFactory.openSession();
//根据id得到省份,然后通过省份点出城市:
IPersonDao iPersonDao=sqlSession.getMapper(IPersonDao.class); Provinces provinces=iPersonDao.getProvinces(1); System.out.println(provinces.getPname()); for (Citys citys : provinces.getCitysSet()) { System.out.println(citys.getCname()); }
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
三:多对多的关系:
多对多的关系我就以Users和Roles用户和角色来演示:
Users表:
private int uid; private String uname; private Set<Roles> roles;
Roles表:
private int rid; private String rname; private Set<Users> users;
然后,再是我的users.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"> <mapper namespace="com.desert.Dao.IUsersDao"> <resultMap type="com.desert.entity.Users" id="usersMap"> <id property="uid" column="uid" /> <result property="uname" column="uname" /> </resultMap> <resultMap type="com.desert.entity.Users" id="usersRolesMap" extends="usersMap"> <collection property="roles" ofType="com.desert.entity.Roles"> <id property="rid" column="rid" /> <result property="rname" column="rname" /> </collection> </resultMap> <select id="selectAllUser" resultMap="usersMap"> select * from users </select> <select id="selectusersRoles" parameterType="int" resultMap="usersRolesMap"> select u.uid,u.uname,r.rid,r.rname from users u left join u_r ur on u.uid=ur.uid left join roles r on ur.rid=r.rid where u.uid =#{uid} </select> </mapper>
再者,是我的roles.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"> <mapper namespace="com.desert.Dao.IRolesDao"> <resultMap type="com.desert.entity.Roles" id="rolesMap"> <id property="rid" column="rid" /> <result property="rname" column="rname" /> </resultMap> <resultMap type="com.desert.entity.Roles" id="rolesUsersMap" extends="rolesMap"> <collection property="users" ofType="com.desert.entity.Users"> <id property="uid" column="uid" /> <result property="uname" column="uname" /> </collection> </resultMap> <select id="selectAllRoles" resultMap="rolesMap"> select * from roles </select> </mapper>
3.接下来就是我的dao层接口:
//根据id得到用户,然后再通过用户点出该用户的所有接口:
public Users selectusersRoles(int uid);
4.然后,去mybatisj-config.xml加载这个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> <!--设置别名--> <typeAliases> <!--<typeAlias type="com.desert.Dto.MyPerson" alias="a"></typeAlias>--> <package name="com.desert"></package> </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/test"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/desert/entity/users.xml"/> <mapper resource="com/desert/entity/roles.xml"/> </mappers> </configuration>
5.现在,就可以开始测试了:
try { SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config")); //通过sessionFactory获得SqlSession SqlSession sqlSession=sqlSessionFactory.openSession();
IUsersDao iUsersDao=sqlSession.getMapper(IUsersDao.class);
//通过根据id得到用户 Users users= (Users) iUsersDao.selectusersRoles(1); System.out.println("用户名"+users.getUname());
//点出所有的角色: for (Roles roles : users.getRoles()) { System.out.println("角色名:"+roles.getRname()); }
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}