mybatisマッピングインターフェース形式の添削

24392 ワード

mybatisマッピングインターフェース形式の添削
  • mybatisマッパーインターフェース形式の添削
  • mybatisマッパーインターフェース形式の検索
  • mybatisマッピングインターフェース形式の添削
    //  
    package com.csdn.mapper;
    
    import com.csdn.bean.User;
    
    public interface UserMapper {
        void insertUserWithId(User user);
        void insertUserByParam(int id, String name, String password);
    }
    
    
    //     
    <?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.csdn.mapper.UserMapper">
        <insert id="insertUserWithId">
            insert into t_person values (#{id},#{name},#{password})
        </insert>
        <insert id="insertUserByParam">
                                          #         param     
            insert into t_person values (#{param1}, #{param2}, #{param3})
        </insert>
    </mapper>
    
    package com.csdn.mapper;
    
    import com.csdn.bean.User;
    import com.csdn.util.DBUtil;
    import org.apache.ibatis.session.SqlSession;
    
    import java.io.IOException;
    
    /**
     * @ClassName
     * @Description TODO
     * @Author sudi
     * @Date 2020/5/3 17:29
     */
    public class UserMapperTest {
        public void insertUser(User user) throws IOException {
            SqlSession sqlSession = null;
            try {
                sqlSession = new DBUtil().getSqlSession();
            
    
    		//  sqlSession              
            //                     
            //             
                sqlSession.insert("insertUserWithId",user);
                sqlSession.commit();
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
    
        public void insertUserByParam(int id, String name ,String password) throws IOException {
            SqlSession sqlSession = null;
            try {
                sqlSession = new DBUtil().getSqlSession();
                UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                //  sqlSession              
                //                     
                //             
                userMapper.insertUserByParam(id, name, password);
                sqlSession.commit();
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
    
        public static void main(String[] args) throws IOException {
            UserMapperTest userMapperTest = new UserMapperTest();
            userMapperTest.insertUser(new User(4,"name","password"));
            userMapperTest.insertUserByParam(3,"name","password");
        }
    }
    
    
    添削と修正はデモンストレーションをしません.
    mybatisマッピングインターフェース形式の検査
    //  
    package com.csdn.mapper;
    
    import com.csdn.bean.User;
    
    import java.util.List;
    
    public interface UserMapper {
        User selectUserById(int id);
        List<User> selectAll();
    }
    
    
    //     
    <?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.csdn.mapper.UserMapper">
        <select id="selectUserById" resultType="com.csdn.bean.User">
            select id, name, password from t_person where id = #{id}
        </select>
        <select id="selectAll" resultType="com.csdn.bean.User">
            select id ,name, password from t_person
        </select>
    </mapper>
    
    package com.csdn.mapper;
    
    import com.csdn.bean.User;
    import com.csdn.util.DBUtil;
    import org.apache.ibatis.session.SqlSession;
    
    import java.io.IOException;
    import java.util.List;
    
    /**
     * @ClassName
     * @Description TODO
     * @Author sudi
     * @Date 2020/5/3 17:29
     */
    public class UserMapperTest {
    
        public User selectUserById(int id) throws IOException {
            SqlSession sqlSession = null;
            try {
                sqlSession = new DBUtil().getSqlSession();
                UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                User user = userMapper.selectUserById(id);
                return user;
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
    
        public List<User> selectAll() throws IOException {
            SqlSession sqlSession = null;
            try {
                sqlSession = new DBUtil().getSqlSession();
                UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                return userMapper.selectAll();
            } finally {
                if (sqlSession != null) {
                    sqlSession.close();
                }
            }
        }
    
        public static void main(String[] args) throws IOException {
            UserMapperTest userMapperTest = new UserMapperTest();
    //        userMapperTest.insertUser(new User(4,"name","password"));
    //        userMapperTest.insertUserByParam(3,"name","password");
            //                
            //          selectOne()    
            User user = userMapperTest.selectUserById(1);
            System.out.println(user);
            //      List  
            List<User> users = userMapperTest.selectAll();
            System.out.println(users);
        }
    }