mybatis&オリジナルメソッドの添削

34940 ワード

mybatis原始的な方法の添削は調べます.
  • mybatis元の方法の削除
  • mybatisのオリジナルメソッドの増加
  • mybatis元の方法の改
  • mybatis原始方法の調査
    //   
    package com.csdn.util;
    
    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 java.io.IOException;
    import java.io.InputStream;
    
    /**
     * @ClassName DBUtil
     * @Description TODO         
     * @Author sudi
     * @Date 2020/5/2 13:50
     */
    public class DBUtil {
        public SqlSession getSqlSession() throws IOException {
            //       
            InputStream inputStream = Resources.getResourceAsStream("com/csdn/config/Configuration.xml");
            //        SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //  SqlSessionFactory       
            return sqlSessionFactory.openSession();
        }
    }
    
    
    //      
    <?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>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC">
                    <property name="" value=""/>
                </transactionManager>
                <dataSource type="UNPOOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/jdbc201910"/>
                    <property name="username" value="root"/>
                    <property name="password" value="sudi"/>
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
    <!--        <mapper resource="org/apache/ibatis/submitted/complex_property/User.xml"/>-->
            <mapper resource="com/csdn/config/sql/User.xml"/>
        </mappers>
    
    </configuration>
    
    mybatis原始的な方法の削除
    //dao 
    package com.csdn.dao;
    
    import com.csdn.util.DBUtil;
    import org.apache.ibatis.session.SqlSession;
    
    import java.io.IOException;
    
    /**
     * @ClassName
     * @Description TODO
     * @Author sudi
     * @Date 2020/5/2 14:09
     */
    public class UserDao {
        public void deleteUserById(int id) throws IOException {
            SqlSession sqlSession = null;
            try {
                sqlSession = new DBUtil().getSqlSession();
                sqlSession.delete("deleteById", id);
                sqlSession.commit();
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
    
        public static void main(String[] args) throws IOException {
            UserDao userDao = new UserDao();
            userDao.deleteUserById(1);
        }
    }
    
    
    //      
    <?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="User">
    <!--    id = #{}        -->
    <delete id="deleteById">
        delete from t_person where id = #{id}
    </delete>
    </mapper>
    
    
    mybatis原始的な方法の増加
    /**
         *       
         * @param id int
         * @param name String
         * @param password String
         */
        public void insertUser(int id, String name, String password) throws IOException {
            SqlSession sqlSession = null;
            try {
                // 1、  sqlSession    ctrl + alt + v
                sqlSession = new DBUtil().getSqlSession();
                // 2、  sqlSession insert  
                sqlSession.insert("insertUser", new User(id, name, password));
                // 3、  
                sqlSession.commit();
            } finally {
                // 4、    
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
    
        /**
         *     
         * @param user User
         */
        public void insertUserByUser(User user) throws IOException {
            SqlSession sqlSession = null;
            try {
                // 1、  sqlSession    ctrl + alt + v
                sqlSession = new DBUtil().getSqlSession();
                // 2、  sqlSession insert  
                sqlSession.insert("insertUserByUser", user);
                // 3、  
                sqlSession.commit();
            } finally {
                // 4、    
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
    
    //      
     <insert id="insertUser" parameterType="com.colin.bean.User">
        insert into user values(#{id}, #{name}, #{password})
      </insert>
      <insert id="insertUserByUser">
        insert into user values(#{id}, #{name}, #{password})
      </insert>
    
  • 注意:複数のパラメータが入ってきた場合、サブプロファイルのi insert into user values(龛{id}、\21879;{name}の着信パラメータ名はUserのメンバー変数名と同じでなければならない.
  • mybatis原始方法の変更
    /**
         *     
         * @param user User
         */
        public void updateUserByUser(User user) throws IOException {
            SqlSession sqlSession = null;
            try {
                sqlSession = new DBUtil().getSqlSession();
                sqlSession.update("updateByUser", user);
                sqlSession.commit();
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
    //Map        
        public void updateUserByMap(Map<String, Object> map) throws IOException {
            SqlSession sqlSession = null;
            try {
                sqlSession = new DBUtil().getSqlSession();
                sqlSession.update("updateUserByMap", map);
                sqlSession.commit();
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
    
         
    		userDao.updateUserByUser(new User(888, "memeda", "dashagua"));
    
            Map<String, Object> map = new HashMap<>();
            map.put("id", 888);
            map.put("password", "yyyy");
            userDao.updateUserByMap(map);
    
    //      
      <update id="updateByUser">
        update user set name = #{name}, password = #{password} where id = #{id}
      </update>
      <update id="updateUserByMap">
        update user set password = #{password} where id = #{id}
      </update>
    
    mybatis原始方法の調査
    //dao 
    //   
        public User selectUserById(int id) throws IOException {
            SqlSession sqlSession = null;
            try {
                // 1、  sqlSession  
                sqlSession = new DBUtil().getSqlSession();
                // 2、  selectOne  
                User user = sqlSession.selectOne("selectUserById", id);
                return user;
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
    
        //   ,            resultType           
        public List<User> selectAll() throws IOException {
            SqlSession sqlSession = null;
            try {
                sqlSession = new DBUtil().getSqlSession();
                List<User> list = sqlSession.selectList("selectAll");
                return list;
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
    
    //     
    <select id="selectUserById" resultType="com.colin.bean.User">
        select id, name, password from user where id = #{fangshadouxing}
      </select>
      <select id="selectAll" resultType="com.colin.bean.User">
        select id, name, password from user
      </select>
    
  • サブプロファイルselectAllのresultTypeはなぜListではなくUserなのですか?
  • はソースコードの中で循環的にデータを調べますので、UserタイプのデータをListの中のadd方法を通して、List作成の対象に一つずつ押し込みます.