package com.mybatis.entity; public class User { private Long userId; private String userName; private Integer userAge; private String userAddress; private Type type; public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getUserAge() { return userAge; } public void setUserAge(Integer userAge) { this.userAge = userAge; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } public Type getType() { return type; } public void setType(Type type) { this.type = type; } @Override public String toString() { return "User [userId=" + userId + ", userName=" + userName + ", userAge=" + userAge + ", userAddress=" + userAddress + ", type=" + type + "]"; } } package com.mybatis.entity; import java.util.HashSet; import java.util.Set; public class Type { private Long typeId; private String typeName; private Set<User> users = new HashSet<User>(); public Long getTypeId() { return typeId; } public void setTypeId(Long typeId) { this.typeId = typeId; } public String getTypeName() { return typeName; } public void setTypeName(String typeName) { this.typeName = typeName; } public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } @Override public String toString() { return "Type [typeId=" + typeId + ", typeName=" + typeName + ", users=" + users + "]"; } } <?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.mybatis.dao.UserMapper"> <!-- 定义用户集合对象 --> <resultMap type="user" id="userMap"> <id column="id" property="userId"/> <result column="userName" property="userName"/> <result column="userAge" property="userAge"/> <result column="userAddress" property="userAddress"/> <!-- 配置多对一的关联 --> <association property="type" column="typeId" javaType="type" select="selectTypeInfoByTypeId"/> </resultMap> <select id="selectAllUsers" resultMap="userMap"> select * from User </select> <select id="selectTypeInfoByTypeId" resultType="type"> select * from type where typeId=#{typeId} </select> <select id="selectUserByUserId" parameterType="user" resultMap="userMap"> <!-- 如果查询语句中只有一个参数可以随便写参数名称 --> select * from user where 1=1 <if test="userName!=null"> AND userName=#{userName} </if> </select> <insert id="addUserInfo"> <!-- 如果语句中存在多个参数,参数名称必须和属性名称一致 --> insert into user values(#{userId},#{userName},#{userAge},#{userAddress},#{type.typeId}) </insert> <update id="updateUserInfo"> update User set userName=#{userName}, userAge=#{userAge}, userAddress=#{userAddress}, typeId=#{type.typeId} where id=#{userId} </update> <delete id="deleteUserByUserId"> delete from user where id = #{id} </delete> </mapper> <?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="typeInfo"> <!-- 定义类型信息对象 --> <resultMap type="type" id="typeMap"> <id column="typeId" property="typeId" /> <result column="typeName" property="typeName"/> <!-- 一对多 --> <collection property="users" column="typeId" ofType="user" select="selectUserByTypeId" /> </resultMap> <select id="selectUserByTypeId" resultMap="com.mybatis.dao.UserMapper.userMap"> select * from User where typeId = #{typeId} </select> <select id="selectTypeInfoByTypeId" resultMap="typeMap"> select * from type where typeId=#{typeId} </select> </mapper> <?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.mybatis.entity.User" alias="user"/> <typeAlias type="com.mybatis.entity.Type" alias="type"/> </typeAliases> <!-- 配置数据源 注意:environment id属性值必须为和default的值相同 --> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC" /> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 导入Mybatis映射文件 --> <mappers> <mapper resource="config/UserMapper.xml"/> <mapper resource="config/TypeMapper.xml"/> </mappers> </configuration> package com.mybatis.dao; import com.mybatis.entity.User; public interface UserMapper { int addUserInfo(User user); } package com.mybatis.test; import java.io.IOException; import java.io.Reader; 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 org.junit.Test; import com.mybatis.dao.UserMapper; import com.mybatis.entity.Type; import com.mybatis.entity.User; public class TestMyBatis { private static SqlSessionFactory sqlSessionFactory; static { try { // 把mybatis-config.xml文件转换为字符流对象 Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private SqlSession getSession() { return sqlSessionFactory.openSession(); } @Test public void test() { System.out.println(sqlSessionFactory); } @Test public void testSelectAll() { List<Object> objects = (List<Object>) getSession().selectList("selectAllUsers"); System.out.println(objects); } @Test public void testSelectUser() { User userInfo = new User(); userInfo.setUserName("赞赏"); List<Object> objects = (List<Object>) getSession().selectList("selectUserByUserId",userInfo); System.out.println(objects); } @Test public void insertUserInfo() { SqlSession session = getSession(); // 创建用户对象 User user = new User(); user.setUserId(40002L); user.setUserName("展示那1"); user.setUserAge(40); user.setUserAddress("重庆1"); Type type = new Type(); type.setTypeId(1003L); user.setType(type); session.insert("addUserInfo",user); session.commit(); session.close(); } @Test public void updateUserInfo() { SqlSession session = getSession(); // 创建用户对象 User user = new User(); user.setUserId(40002L); user.setUserName("赞赏"); user.setUserAge(40); user.setUserAddress("重庆1"); Type type = new Type(); type.setTypeId(1003L); user.setType(type); session.update("updateUserInfo",user); session.commit(); session.close(); } @Test public void deletetUser() { SqlSession session = getSession(); int row = session.delete("deleteUserByUserId",40001); session.commit(); session.close(); System.out.println(row>0?"成功":"失败"); } @Test // 测试一对多的管理查询 public void selectTypeByTypeId() { SqlSession session = getSession(); Type type = session.selectOne("typeInfo.selectTypeInfoByTypeId", 1003); System.out.println(type); } @Test // 开发中常用方法 public void testAddUserInfo() { SqlSession session = getSession(); UserMapper userMapper = session.getMapper(UserMapper.class); User user = new User(); user.setUserId(40005L); user.setUserName("fdsfds"); user.setUserAge(40); user.setUserAddress("hgfgd"); Type type = new Type(); type.setTypeId(1003L); user.setType(type); System.out.println(userMapper.addUserInfo(user)); session.commit(); session.close(); } }
时间: 2024-10-08 20:04:22