标签:mamicode strong log4j char param factory user ali username
学习过的持久层框架:DBUtils , Hibernate
Mybatis就是类似于hibernate的orm持久层框架。
为什么学Mybatis?
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。
Mybatis是面向sql的持久层框架,他封装了jdbc访问数据库的过程,我们开发,只需专注于sql语句本身的拼装,其它复杂的过程全部可以交给mybatis去完成。
根据用户ID查询用户信息
根据用户名查找用户列表
添加用户
修改用户
删除用户
<!-- id:statementId resultType:查询结果集的数据类型 parameterType:查询的入参 --> <select id="getUserById" parameterType="int" resultType="com.itheima.mybatis.pojo.User" > SELECT * FROM USER WHERE id = #{id1} </select>
@Test public void testGetUserByid() throws IOException { // 创建SqlSessionFactoryBuilder对象 SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder(); // 查找配置文件创建输入流 InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); // 加载配置文件,创建SqlSessionFactory对象 SqlSessionFactory sqlSessionFactory = sfb.build(inputStream); // 创建SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); // 执行查询,参数一:要查询的statementId ,参数二:sql语句入参 User user = sqlSession.selectOne("user.getUserById", 1); // 输出查询结果 System.out.println(user); // 释放资源 sqlSession.close(); }
/** * SqlSessionFactory工具类 * * @author Steven * */ public class SqlSessionFactoryUtils { /** * 单例SqlSessionFactory */ private static SqlSessionFactory sqlSessionFactory; static { // 创建SqlSessionFactoryBuilder对象 SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder(); try { // 查找配置文件创建输入流 InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); // 加载配置文件,创建SqlSessionFactory对象 sqlSessionFactory = sfb.build(inputStream); } catch (IOException e) { e.printStackTrace(); } } /** * 获取单例SqlSessionFactory * @return */ public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } }
<!-- resultType:如果要返回数据集合,只需设定为每一个元素的数据类型 --> <select id="getUserByName" parameterType="string" resultType="com.itheima.mybatis.pojo.User"> <!-- SELECT * FROM USER WHERE username LIKE #{name} --> SELECT * FROM USER WHERE username LIKE ‘%${value}%‘ </select>
@Test public void getUserByName() { SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(); //List<User> users = sqlSession.selectList("user.getUserByName", "%张%"); List<User> users = sqlSession.selectList("user.getUserByName", "张"); for (User user : users) { System.out.println(user); } // 释放资源 sqlSession.close(); } 4.3.5
添加用户
<insert id="insertUser" parameterType="com.itheima.mybatis.pojo.User"> INSERT INTO USER (`username`, `birthday`, `sex`, `address`) VALUES (#{username}, #{birthday}, #{sex}, #{address}); </insert>
@Test public void testInsertUser() throws IOException { // 创建SqlSession对象 SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(); User user = new User(); user.setUsername("张飞"); user.setAddress("深圳市黑马"); user.setBirthday(new Date()); user.setSex("1"); // 执行插入 sqlSession.insert("user.insertUser", user); // 提交事务 sqlSession.commit(); // 释放资源 sqlSession.close(); }
<!-- useGeneratedKeys:标识插入使用自增id keyProperty:与useGeneratedKeys配套使用,用于绑定主键接收的pojo属性 --> <insert id="insertUserKey" parameterType="com.itheima.mybatis.pojo.User" useGeneratedKeys="true" keyProperty="id"> <!-- selectKey:用于配置主键返回 keyProperty:要绑定的pojo属性 resultType:属性数据类型 order:指定什么时候执行,AFTER之后 --> <!-- <selectKey keyProperty="id" resultType="int" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> --> INSERT INTO USER (`username`, `birthday`, `sex`, `address`, `uuid2`) VALUES (#{username}, #{birthday}, #{sex}, #{address}, #{uuid2}); </insert>
4.3.5.4Mysql的uuid返回主键
注:在使用uuid之前数据库user表要先加上uuid2字段、user的pojo也要加上相应属性
<!-- useGeneratedKeys:标识插入使用自增id keyProperty:与useGeneratedKeys配套使用,用于绑定主键接收的pojo属性 --> <insert id="insertUserUUID" parameterType="com.itheima.mybatis.pojo.User"> <!-- selectKey:用于配置主键返回 keyProperty:要绑定的pojo属性 resultType:属性数据类型 order:指定什么时候执行,BEFORE之前 --> <selectKey keyProperty="uuid2" resultType="string" order="BEFORE"> SELECT UUID() </selectKey> INSERT INTO USER (`username`, `birthday`, `sex`, `address`, `uuid2`) VALUES (#{username}, #{birthday}, #{sex}, #{address}, #{uuid2}); </insert>
<update id="updateUser" parameterType="com.itheima.mybatis.pojo.User"> UPDATE USER SET username = #{username} WHERE id = #{id} </update>
<delete id="deleteUser" parameterType="int"> DELETE FROM `user` WHERE `id` = #{id1} </delete>
根据用户ID查询用户信息
根据用户名查找用户列表
添加用户
public interface UserDao { /**根据用户ID查询用户信息 * @param id * @return */ User getUserById(Integer id); /** * 根据用户名查找用户列表 * @param name * @return */ List<User> getUserByUserName(String name); /** * 添加用户 * @param user */ void insertUser(User user); }
public class UserDaoImpl implements UserDao { @Override public User getUserById(Integer id) { SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(); User user = sqlSession.selectOne("user.getUserById", id); sqlSession.close(); return user; } @Override public List<User> getUserByUserName(String name) { SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(); List<User> list = sqlSession.selectList("user.getUserByName", name); sqlSession.close(); return list; } @Override public void insertUser(User user) { SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(); sqlSession.insert("user.insertUser", user); sqlSession.commit(); sqlSession.close(); } }
public class UserDaoTest { @Test public void testGetUserById() { UserDao userDao = new UserDaoImpl(); User user = userDao.getUserById(30); System.out.println(user); } @Test public void testGetUserByUserName() { UserDao userDao = new UserDaoImpl(); List<User> list = userDao.getUserByUserName("张"); for (User user : list) { System.out.println(user); } } @Test public void testInsertUser() { UserDao userDao = new UserDaoImpl(); User user = new User(); user.setUsername("张飞3"); user.setAddress("深圳市黑马"); user.setBirthday(new Date()); user.setSex("1"); userDao.insertUser(user); } }
public class UserMapperTest { @Test public void testGetUserById() { // 加载配置得到SqlSession SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(); // 获取代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // 查询数据 User user = userMapper.getUserById(30); System.out.println(user); // 关闭资源 sqlSession.close(); } @Test public void testGetUserByUserName() { SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(); // 获取代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // 查询数据 List<User> list = userMapper.getUserByName("张"); for (User user : list) { System.out.println(user); } // 关闭资源 sqlSession.close(); } @Test public void testInsertUser() { SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User(); user.setUsername("张飞飞"); user.setAddress("深圳市黑马"); user.setBirthday(new Date()); user.setSex("1"); userMapper.insertUser(user); // 提交事务 sqlSession.commit(); // 关闭资源 sqlSession.close(); } }
<!-- 加载规则,首先加载标签内部属性,再加载外部文件,名称相同时,会替换相同名称的内容 --> <properties resource="jdbc.properties"> <property name="jdbc.username" value="root1"/> <property name="jdbc.password" value="root"/> </properties>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
mybatis默认支持java基本数据类型的别名识别详细参考教案。
自定义别名
<typeAliases> <!-- 单个别名定义 --> <!-- <typeAlias type="com.itheima.mybatis.pojo.User" alias="user"/> --> <!-- 别名包扫描器(推荐使用此方式),整个包下的类都被定义别名,别名为类名,不区分大小写--> <package name="com.itheima.mybatis.pojo"/> </typeAliases>
<mappers> <!-- 第一种方式,加载 resource--> <mapper resource="mapper/user.xml"/> <!-- <mapper resource="mapper/UserMapper.xml"/> --> <!-- 第二种方式,class扫描器要求: 1、映射文件与接口同一目录下 2、映射文件名必需与接口文件名称一致 --> <!-- <mapper class="com.itheima.mybatis.mapper.UserMapper"/> --> <!-- 第三种方式,包扫描器要求(推荐使用此方式): 1、映射文件与接口同一目录下 2、映射文件名必需与接口文件名称一致 --> <package name="com.itheima.mybatis.mapper"/> </mappers>
标签:mamicode strong log4j char param factory user ali username
原文地址:https://www.cnblogs.com/HooDee/p/11077692.html