码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis的学习

时间:2020-06-03 15:52:40      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:roo   property   commit   ssl   delete   direct   test   lis   type   

<!--核心配置文件-->
<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>
以下是测试连接代码
@Test
public 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();
}

mybatis的学习

标签:roo   property   commit   ssl   delete   direct   test   lis   type   

原文地址:https://www.cnblogs.com/xinchengTan/p/13037659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!