mybatis基础学习二

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis基础学习二相关的知识,希望对你有一定的参考价值。

操作users表的CRUD

XML实现方式

  • 定义sql映射xml文件 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. <mapper namespace="mybatis.userMapper">
  4. <!-- <select id="getid" parameterType="int"
  5. resultType="mybatis.User">
  6. select * from users where id=#{id}
  7. </select> -->
  8. <insert id="insertUser" parameterType="mybatis.User">
  9. insert into users(name, age) values(#{name}, #{age});
  10. </insert>
  11. <delete id="deleteUser" parameterType="int">
  12. delete from users where id=#{id}
  13. </delete>
  14. <update id="updateUser" parameterType="mybatis.User">
  15. update users set name=#{name},age=#{age} where id=#{id}
  16. </update>
  17. <select id="selectUser" parameterType="int" resultType="mybatis.User">
  18. select * from users where id=#{id}
  19. </select>
  20. <select id="selectAllUsers" resultType="mybatis.User">
  21. select * from users
  22. </select>
  23. </mapper>
  • 在conf.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. <configuration>
  4. <environments default="development">
  5. <environment id="development">
  6. <transactionManager type="JDBC" />
  7. <dataSource type="POOLED">
  8. <property name="driver" value="com.mysql.jdbc.Driver" />
  9. <property name="url" value="jdbc:mysql://172.171.2.161:3306/mybatis" />
  10. <property name="username" value="root" />
  11. <property name="password" value="root123" />
  12. </dataSource>
  13. </environment>
  14. </environments>
  15. <mappers>
  16. <!--xml方式 -->
  17. <mapper resource="mybatis/userMapper.xml"/>
  18. </mappers>
  19. </configuration>
  • 封装得到session的方法MybatisUtil.java
  1. package mybatis;
  2. import java.io.InputStream;
  3. import org.apache.ibatis.session.SqlSession;
  4. import org.apache.ibatis.session.SqlSessionFactory;
  5. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  6. public class MybatisUtil
  7. {
  8. //返回session
  9. public static SqlSession openSqlSession()
  10. {
  11. String resource = "conf.xml";
  12. InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
  13. SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
  14. SqlSession session = factory.openSession();
  15. //貌似可有可无
  16. session.commit();
  17. return session;
  18. }
  19. }
  • 编写测试类 Test.java

插入用户

  1. package mybatis;
  2. import java.io.IOException;
  3. public class Test
  4. {
  5. public static void main(final String[] args) throws IOException
  6. {
  7. /**
  8. * xml方式测试
  9. */
  10. String statement = "mybatis.userMapper.insertUser";
  11. int tuser = MybatisUtil.openSqlSession().insert(statement, new User(3, "rose", 3));
  12. MybatisUtil.openSqlSession().close();
  13. System.out.println(tuser);
  14. }
  15. }

更新用户

  1. package mybatis;
  2. import java.io.IOException;
  3. public class Test
  4. {
  5. public static void main(final String[] args) throws IOException
  6. {
  7. /**
  8. * xml方式测试
  9. */
  10. String statement = "mybatis.userMapper.updateUser";
  11. int tuser = MybatisUtil.openSqlSession().update(statement, new User(3, "rose", 3));
  12. MybatisUtil.openSqlSession().close();
  13. System.out.println(tuser);
  14. }
  15. }

删除用户

  1. package mybatis;
  2. import java.io.IOException;
  3. public class Test
  4. {
  5. public static void main(final String[] args) throws IOException
  6. {
  7. /**
  8. * xml方式测试
  9. */
  10. String statement = "mybatis.userMapper.deleteUser";
  11. int tuser = MybatisUtil.openSqlSession().delete(statement,2));
  12. MybatisUtil.openSqlSession().close();
  13. System.out.println(tuser);
  14. }
  15. }

查询全部用户

  1. package mybatis;
  2. import java.io.IOException;
  3. public class Test
  4. {
  5. public static void main(final String[] args) throws IOException
  6. {
  7. /**
  8. * xml方式测试
  9. */
  10. String statement = "mybatis.userMapper.selectAllUsers";
  11. List<User> tuser = MybatisUtil.openSqlSession().selectList(statement);
  12. MybatisUtil.openSqlSession().close();
  13. System.out.println(tuser);
  14. }
  15. }

注解实现方式

  • 定义sql接口文件 UserMapperDe.java
  1. package mybatis;
  2. import java.util.List;
  3. import org.apache.ibatis.annotations.Delete;
  4. import org.apache.ibatis.annotations.Insert;
  5. import org.apache.ibatis.annotations.Select;
  6. import org.apache.ibatis.annotations.Update;
  7. public interface UserMapperDe
  8. {
  9. @Insert("insert into users(name, age) values(#{name}, #{age})")
  10. public int insertUser(User user);
  11. @Delete("delete from users where id=#{id}")
  12. public int deleteUserById(int id);
  13. @Update("update users set name=#{name},age=#{age} where id=#{id}")
  14. public int updateUser(User user);
  15. @Select("select * from users where id=#{id}")
  16. public User getUserById(int id);
  17. @Select("select * from users")
  18. public List<User> getAllUser();
  19. }
  • 在conf.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. <configuration>
  4. <environments default="development">
  5. <environment id="development">
  6. <transactionManager type="JDBC" />
  7. <dataSource type="POOLED">
  8. <property name="driver" value="com.mysql.jdbc.Driver" />
  9. <property name="url" value="jdbc:mysql://172.171.2.161:3306/mybatis" />
  10. <property name="username" value="root" />
  11. <property name="password" value="root123" />
  12. </dataSource>
  13. </environment>
  14. </environments>
  15. <mappers>
  16. <!--xml方式 -->
  17. <mapper resource="mybatis/userMapper.xml"/>
  18. <!--注解方式 -->
  19. <mapper class="mybatis.UserMapperDe"/>
  20. </mappers>
  21. </configuration>
  • 编写测试类 TestClass.java

插入用户

  1. package mybatis;
  2. import java.io.IOException;
  3. public class Test
  4. {
  5. public static void main(final String[] args) throws IOException
  6. {
  7. /**
  8. * 注解方式测试
  9. */
  10. UserMapperDe mapper = MybatisUtil.openSqlSession().getMapper(UserMapperDe.class);
  11. int inuser = mapper.insertUser(new User(-1, "clock", 1));
  12. MybatisUtil.openSqlSession().close();
  13. System.out.println(inuser);
  14. }
  15. }

更新用户

  1. package mybatis;
  2. import java.io.IOException;
  3. public class Test
  4. {
  5. public static void main(final String[] args) throws IOException
  6. {
  7. /**
  8. * 注解方式测试
  9. */
  10. UserMapperDe mapper = MybatisUtil.openSqlSession().getMapper(UserMapperDe.class);
  11. int inuser = mapper.updateUser(new User(2, "efpd", 2));
  12. MybatisUtil.openSqlSession().close();
  13. System.out.println(inuser);
  14. }
  15. }

删除用户

  1. package mybatis;
  2. import java.io.IOException;
  3. public class Test
  4. {
  5. public static void main(final String[] args) throws IOException
  6. {
  7. /**
  8. * 注解方式测试
  9. */
  10. UserMapperDe mapper = MybatisUtil.openSqlSession().getMapper(UserMapperDe.class);
  11. int inuser = mapper.deleteUserById(2);
  12. MybatisUtil.openSqlSession().close();
  13. System.out.println(inuser);
  14. }
  15. }

查询全部用户

  1. package mybatis;
  2. import java.io.IOException;
  3. public class Test
  4. {
  5. public static void main(final String[] args) throws IOException
  6. {
  7. /**
  8. * 注解方式测试
  9. */
  10. UserMapperDe mapper = MybatisUtil.openSqlSession().getMapper(UserMapperDe.class);
  11. List<User> inuser = mapper.getAllUser();
  12. MybatisUtil.openSqlSession().close();
  13. System.out.println(inuser);
  14. }
  15. }




以上是关于mybatis基础学习二的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis学习总结——使用MyBatis对表执行CRUD操作

MyBatis学习总结——使用MyBatis对表执行CRUD操作

Mybatis注解开发

MybatisMyBatis对表执行CRUD操作

Mybatis学习 - CRUD操作(增删改查操作)

MyBatis第三课 MyBatis映射文件的CRUD操作<方式二>