Browse Source

Mybatis配置文件用config存储

lichunlei 1 year ago
parent
commit
b0926deb18

+ 6 - 0
src/main/java/com/ym/adminap/config/MybatisConfig.java

@@ -0,0 +1,6 @@
+package com.ym.adminap.config;
+
+public class MybatisConfig {
+
+	public static String mainConfigFileName = "mybatis-config.xml";
+}

+ 12 - 11
src/main/java/com/ym/adminap/service/main/UsersService.java

@@ -7,6 +7,7 @@ import java.util.Map;
 import org.apache.ibatis.session.SqlSession;
 
 import com.alibaba.fastjson.JSONObject;
+import com.ym.adminap.config.MybatisConfig;
 import com.ym.adminap.dao.main.UsersMapper;
 import com.ym.adminap.enity.ListParam;
 import com.ym.adminap.enity.RelationData;
@@ -34,7 +35,7 @@ public class UsersService {
         if (fields.isBlank()) {
             fields = "*";
         }
-        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
+        SqlSession sqlSession = SqlSessionUtils.getSqlSession(MybatisConfig.mainConfigFileName);
         UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
         ListParam param = new ListParam();
         if (page > 1) {
@@ -80,7 +81,7 @@ public class UsersService {
      * @return
      */
     public static Users query(Integer Id) {
-        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
+        SqlSession sqlSession = SqlSessionUtils.getSqlSession(MybatisConfig.mainConfigFileName);
         UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
         Users result = usersMapper.selectByPrimaryKey(Id);
         sqlSession.close();
@@ -95,7 +96,7 @@ public class UsersService {
      * @return
      */
     public static Users query(Users data) {
-        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
+        SqlSession sqlSession = SqlSessionUtils.getSqlSession(MybatisConfig.mainConfigFileName);
         UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
         Users result = usersMapper.selectByCondition(data);
         if (result == null)
@@ -113,7 +114,7 @@ public class UsersService {
      * @return
      */
     public static Integer count(String field, Users data, String condition) {
-        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
+        SqlSession sqlSession = SqlSessionUtils.getSqlSession(MybatisConfig.mainConfigFileName);
         UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
         Integer result = usersMapper.getCount(field, data, condition);
         sqlSession.close();
@@ -129,7 +130,7 @@ public class UsersService {
      * @return
      */
     public static Object sum(String field, Users data, String condition) {
-        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
+        SqlSession sqlSession = SqlSessionUtils.getSqlSession(MybatisConfig.mainConfigFileName);
         UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
         Object result = usersMapper.getSum(field, data, condition);
         sqlSession.close();
@@ -144,7 +145,7 @@ public class UsersService {
      * @return
      */
     public static Boolean exist(Users data, String condition) {
-        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
+        SqlSession sqlSession = SqlSessionUtils.getSqlSession(MybatisConfig.mainConfigFileName);
         UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
         Integer result = usersMapper.getCount("1", data, condition);
         sqlSession.close();
@@ -161,7 +162,7 @@ public class UsersService {
      * @return
      */
     public static Integer add(Users data) {
-        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
+        SqlSession sqlSession = SqlSessionUtils.getSqlSession(MybatisConfig.mainConfigFileName);
         UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
         Integer result = usersMapper.insert(data);
         sqlSession.close();
@@ -175,7 +176,7 @@ public class UsersService {
      * @return
      */
     public static Integer update(Users data) {
-        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
+        SqlSession sqlSession = SqlSessionUtils.getSqlSession(MybatisConfig.mainConfigFileName);
         UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
         Integer result = usersMapper.updateByPrimaryKey(data);
         sqlSession.close();
@@ -192,7 +193,7 @@ public class UsersService {
         Users data = new Users();
         data.setId(id);
         data.setStatus(-1);
-        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
+        SqlSession sqlSession = SqlSessionUtils.getSqlSession(MybatisConfig.mainConfigFileName);
         UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
         Integer result = usersMapper.updateByPrimaryKey(data);
         sqlSession.close();
@@ -206,7 +207,7 @@ public class UsersService {
      * @return
      */
     public static Integer delete(Integer id) {
-        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
+        SqlSession sqlSession = SqlSessionUtils.getSqlSession(MybatisConfig.mainConfigFileName);
         UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
         Integer result = usersMapper.deleteByPrimaryKey(id);
         sqlSession.close();
@@ -224,7 +225,7 @@ public class UsersService {
         Users user = new Users();
         user.setId(id);
         user.setSort(sort);
-        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
+        SqlSession sqlSession = SqlSessionUtils.getSqlSession(MybatisConfig.mainConfigFileName);
         UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
         Integer result = usersMapper.updateByPrimaryKey(user);
         sqlSession.close();

+ 2 - 2
src/main/java/com/ym/adminap/util/SqlSessionUtils.java

@@ -9,10 +9,10 @@ import java.io.IOException;
 import java.io.InputStream;
 
 public class SqlSessionUtils {
-    public static SqlSession getSqlSession() {
+    public static SqlSession getSqlSession(String xmlFileName) {
         SqlSession sqlSession = null;
         try {
-            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
+            InputStream inputStream = Resources.getResourceAsStream(xmlFileName);
             SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
             sqlSession = sqlSessionFactory.openSession(true);
         } catch (IOException e) {