mybatis的学习

<!--核心配置文件--><configuration>    <environments default="development">        <environment id="development">            <transactionManager type="JDBC"/>            <dataSource type="POOLED">                <property name="driver" value="com.mysql.jdbc.Driver"/>                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>                <property name="username" value="root"/>                <property name="password" value="123456"/>            </dataSource>        </environment>
<!--在build中配置resources,来防止我们的资源导出失败的问题-->    <build>        <resources>            <resource>                <directory>src/main/java</directory>                <includes>                    <include>**/*.properties</include>                    <include>**/*.xml</include>                </includes>                <filtering>false</filtering>            </resource>            <resource>                <directory>src/main/resources</directory>                <includes>                    <include>**/*.properties</include>                    <include>**/*.xml</include>                </includes>                <filtering>false</filtering>            </resource>        </resources>    </build>以下是测试连接代码
@Testpublic void  test1(){    //获取sqlsession对象    SqlSession sqlsession = mybatisutis.getsqlsession();    //执行sql    UserDao userDao = sqlsession.getMapper(UserDao.class);    List<User> userList = userDao.getUserList();    for (User user : userList) {        System.out.println(user);    }    //关闭sqlsession    sqlsession.close();}
public class UserMapperTest {    //查询所有用户的测试类    @Test    public void  test1(){        //获取sqlsession对象        SqlSession sqlsession = mybatisutis.getsqlsession();        //执行sql        UserMapper userMapper = sqlsession.getMapper(UserMapper.class);        List<User> userList = userMapper.getUserList();        for (User user : userList) {            System.out.println(user);        }        //关闭sqlsession        sqlsession.close();    }    //根据id查询某个用户    @Test    public  void  test2(){        //获取sql执行对象        SqlSession getsqlsession = mybatisutis.getsqlsession();        //执行sql        UserMapper mapper = getsqlsession.getMapper(UserMapper.class);        User userByid = mapper.getuserByid(1);        System.out.println(userByid);        //关闭连接        getsqlsession.close();    }    //添加用户    @Test    public  void  test3(){        SqlSession getsqlsession2 = mybatisutis.getsqlsession();        UserMapper mapper = getsqlsession2.getMapper(UserMapper.class);        int i = mapper.addUser(new User(5, "lixiang", "123"));        if (i>0){            System.out.println("添加成功");        }        //提交事务        getsqlsession2.commit();        getsqlsession2.close();    }    //修改用户    @Test    public  void  test4(){        SqlSession getsqlsession = mybatisutis.getsqlsession();        UserMapper mapper = getsqlsession.getMapper(UserMapper.class);        mapper.updete(new User(3,"何峰","123"));        getsqlsession.commit();        getsqlsession.close();    }    //删除用户    @Test    public  void  test5(){        SqlSession getsqlsession = mybatisutis.getsqlsession();        UserMapper mapper = getsqlsession.getMapper(UserMapper.class);        mapper.deleteuser(3);        getsqlsession.commit();        getsqlsession.close();    }

相关推荐