Compare commits
1 Commits
master
...
af500ad757
Author | SHA1 | Date | |
---|---|---|---|
af500ad757 |
@ -1,114 +0,0 @@
|
|||||||
package com.example.practice.sqlsession;
|
|
||||||
|
|
||||||
import com.example.practice.entity.User;
|
|
||||||
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;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class SqlSessionTest {
|
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
|
||||||
String path = "mybatis-config.xml";
|
|
||||||
// 文件字节流
|
|
||||||
InputStream is = Resources.getResourceAsStream(path);
|
|
||||||
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
|
|
||||||
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
|
|
||||||
// 数据库连接对象,类似于JDBC中的Connection对象, 每调用一次都会创建一次(使用连接池可进行复用)
|
|
||||||
SqlSession sqlSession = sqlSessionFactory.openSession();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 插入
|
|
||||||
User insertUser = new User();
|
|
||||||
insertUser.setId(null);
|
|
||||||
insertUser.setNickName("管理员2");
|
|
||||||
insertUser.setPhoneNumber("12345678901");
|
|
||||||
insertUser.setUserAccount("admin2");
|
|
||||||
insertUser.setUserPassword("12345678");
|
|
||||||
insertUser.setUserRole("admin");
|
|
||||||
insert(sqlSession, insertUser);
|
|
||||||
|
|
||||||
|
|
||||||
// 根据id删除
|
|
||||||
delete(sqlSession, 12L);
|
|
||||||
|
|
||||||
|
|
||||||
// 修改
|
|
||||||
User updateUser = new User();
|
|
||||||
updateUser.setId(11L);
|
|
||||||
updateUser.setNickName("管理员dsfaf2");
|
|
||||||
updateUser.setPhoneNumber("fdsdsgsfdsf");
|
|
||||||
updateUser.setUserAccount("fds");
|
|
||||||
updateUser.setUserPassword("12345678");
|
|
||||||
updateUser.setUserRole("admin");
|
|
||||||
update(sqlSession, updateUser);
|
|
||||||
|
|
||||||
|
|
||||||
// 查询
|
|
||||||
selectList(sqlSession);
|
|
||||||
|
|
||||||
// 根据id查询
|
|
||||||
selectOne(sqlSession, 4L);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static void insert(SqlSession sqlSession, User user) {
|
|
||||||
try {
|
|
||||||
int result = sqlSession.insert("fdeawgryer.insert", user);
|
|
||||||
if (result == 0) throw new RuntimeException("添加失败");
|
|
||||||
sqlSession.commit();
|
|
||||||
} catch (Exception e) {
|
|
||||||
sqlSession.rollback();
|
|
||||||
} finally {
|
|
||||||
sqlSession.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static void delete(SqlSession sqlSession, Long id) {
|
|
||||||
try {
|
|
||||||
int result = sqlSession.delete("fdeawgryer.delete", id);
|
|
||||||
if (result == 0) throw new RuntimeException("删除失败");
|
|
||||||
sqlSession.commit();
|
|
||||||
} catch (Exception e) {
|
|
||||||
sqlSession.rollback();
|
|
||||||
} finally {
|
|
||||||
sqlSession.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static void update(SqlSession sqlSession, User user) {
|
|
||||||
try {
|
|
||||||
int result = sqlSession.update("fdeawgryer.update", user);
|
|
||||||
if (result == 0) throw new RuntimeException("更新失败");
|
|
||||||
sqlSession.commit();
|
|
||||||
} catch (Exception e) {
|
|
||||||
sqlSession.rollback();
|
|
||||||
} finally {
|
|
||||||
sqlSession.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static void selectList(SqlSession sqlSession) {
|
|
||||||
List<User> userList = sqlSession.selectList("fdeawgryer.dfsakdf");
|
|
||||||
for (User user : userList) {
|
|
||||||
System.out.println(user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static void selectOne(SqlSession sqlSession, Long id) {
|
|
||||||
User user = sqlSession.selectOne("fdeawgryer.selectOne", id);
|
|
||||||
System.out.println(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,76 @@
|
|||||||
|
package com.example.practice.sqlsessionTest;
|
||||||
|
|
||||||
|
import com.example.practice.entity.User;
|
||||||
|
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;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SqlSessionTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
|
||||||
|
// 加载配置文件(转成字节流的形式)
|
||||||
|
String resourcePath = "mybatis-config.xml";
|
||||||
|
InputStream is = Resources.getResourceAsStream(resourcePath);
|
||||||
|
|
||||||
|
// SqlSessionFactoryBuilder负责创建SqlSessionFactory对象
|
||||||
|
// SqlSessionFactory负责生产SqlSession对象
|
||||||
|
// SqlSession对象是负责连接数据库, 每执行一条sql语句,就会创建或者连接池中获取一个对象
|
||||||
|
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
|
||||||
|
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
|
||||||
|
SqlSession sqlSession = sqlSessionFactory.openSession();
|
||||||
|
|
||||||
|
// List<User> list = sqlSession.selectList("fdsgfgd.selectList");
|
||||||
|
// for (User user : list) {
|
||||||
|
// System.out.println(user);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// User user = sqlSession.selectOne("fdsgfgd.selectOne", 2);
|
||||||
|
// System.out.println(user);
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// int delete = sqlSession.delete("fdsgfgd.delete", 2);
|
||||||
|
// if (delete == 0) throw new RuntimeException("删除失败");
|
||||||
|
// sqlSession.commit();
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// User user = new User();
|
||||||
|
// user.setId(null);
|
||||||
|
// user.setNickName("张三");
|
||||||
|
// user.setPhoneNumber("12345678901");
|
||||||
|
// user.setUserAccount("123456");
|
||||||
|
// user.setUserPassword("123456");
|
||||||
|
// user.setUserRole("user");
|
||||||
|
// user.setMoney(10000);
|
||||||
|
// sqlSession.insert("fdsgfgd.insert", user);
|
||||||
|
// sqlSession.commit();
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// User user = new User();
|
||||||
|
// user.setId(9L);
|
||||||
|
// user.setNickName("张三dfsafda");
|
||||||
|
// user.setPhoneNumber("12345678901");
|
||||||
|
// user.setUserAccount("123456");
|
||||||
|
// user.setUserPassword("123456");
|
||||||
|
// user.setUserRole("user");
|
||||||
|
// user.setMoney(10000);
|
||||||
|
// sqlSession.update("fdsgfgd.update", user);
|
||||||
|
// sqlSession.commit();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// public static void selectList() {
|
||||||
|
// List<User> list = sqlSession.selectList("fdsgfgd.selectList");
|
||||||
|
// for (User user : list) {
|
||||||
|
// System.out.println(user);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
@ -2,7 +2,7 @@
|
|||||||
<!DOCTYPE mapper
|
<!DOCTYPE mapper
|
||||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="sdfdsafdfd">
|
<mapper namespace="fdsgfgd">
|
||||||
<select id="selectList" resultType="com.example.practice.entity.User">
|
<select id="selectList" resultType="com.example.practice.entity.User">
|
||||||
select * from user
|
select * from user
|
||||||
</select>
|
</select>
|
||||||
@ -24,9 +24,6 @@
|
|||||||
</update>
|
</update>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|
||||||
<!--SQL映射文件-->
|
<!--SQL映射文件-->
|
@ -2,8 +2,11 @@
|
|||||||
<!DOCTYPE configuration
|
<!DOCTYPE configuration
|
||||||
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
||||||
"http://mybatis.org/dtd/mybatis-3-config.dtd">
|
"http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||||
|
|
||||||
<configuration>
|
<configuration>
|
||||||
|
|
||||||
<environments default="dev">
|
<environments default="dev">
|
||||||
|
|
||||||
<environment id="dev">
|
<environment id="dev">
|
||||||
<transactionManager type="JDBC"/>
|
<transactionManager type="JDBC"/>
|
||||||
<dataSource type="POOLED">
|
<dataSource type="POOLED">
|
||||||
@ -13,6 +16,7 @@
|
|||||||
<property name="password" value="123456"/>
|
<property name="password" value="123456"/>
|
||||||
</dataSource>
|
</dataSource>
|
||||||
</environment>
|
</environment>
|
||||||
|
|
||||||
</environments>
|
</environments>
|
||||||
|
|
||||||
<mappers>
|
<mappers>
|
||||||
@ -20,4 +24,5 @@
|
|||||||
</mappers>
|
</mappers>
|
||||||
|
|
||||||
</configuration>
|
</configuration>
|
||||||
<!--Mybatis核心配置文件-->
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user