为什么MyBatis和Spring自动装载我的Mapper?(Why is my Spring Boot @Autowired MyBatis static mapper null?)
我有以下类,但Spring和MyBatis-Spring-Boot-Starter不会自动连接我的映射器。
当我运行请求时,我得到了
println()
的输出sourceMapper = null
类
package ...model; import java.net.MalformedURLException; import java.net.URL; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import ...mapper.SourceMapper; public class Source { public long id; public String host; public Date created_at; @Autowired public static SourceMapper sourceMapper; #### Why isn't this set? public static Source findOrCreate(String url) { Source source = null; String host = "NONE"; if (url != null) { // extract only the domain try { URL urlObj = new URL(url); host = urlObj.getHost(); } catch (MalformedURLException e) { } // ignore } System.out.println("sourceMapper = " + sourceMapper); source = sourceMapper.findByHost(host); if (source == null) { long newId = sourceMapper.create(host); source = sourceMapper.findById(newId); } return source; } }
我尽可能接近这些例子。
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
处理请求的主
@Controller
类中的其他@Autowired
Mappers
工作,即使它们是私有的。这是Mapper类
package ...mapper; @Mapper public interface SourceMapper { ...
MyBatis 3.4.5,MyBatis Spring 1.3.1,MyBatis Spring Boot Autoconfigure 1.3.1,MyBatis Spring Boot Starter 1.3.1
I have the following class, but Spring and MyBatis-Spring-Boot-Starter will not autowire my mapper.
When I run the request, I get the output from the
println()
sourceMapper = null
Model
public class Source { @Autowired public static SourceMapper sourceMapper; #### Why isn't this set? public static Source findOrCreate(String url) { ... System.out.println("sourceMapper = " + sourceMapper); source = sourceMapper.findByHost(host); ... } }
I followed the examples as closely as possible.
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
The other
@Autowired
Mappers
in the main@Controller
class that handles requests work, even though they are private.This is the Mapper class
package ...mapper; @Mapper public interface SourceMapper { ...
I ran into this issue again with a new model and mapper. I tried to follow Why is my Spring @Autowired field null? and the code sample, but it was still null! I tried
@Configurable
,@Service
,@Component
.Model
@Configurable public class Domain { @Autowired private static DomainMapper domainMapper; public static void incrementCounter(String host) { ... Domain d = getDomainMapper().find(host, thisMonth); public static DomainMapper getDomainMapper() { return domainMapper; public static void setDomainMapper(DomainMapper domainMapper) { Domain.domainMapper = domainMapper;
Mapper
@Mapper public interface DomainMapper {
MyBatis 3.4.5, MyBatis Spring 1.3.1, MyBatis Spring Boot Autoconfigure 1.3.1, MyBatis Spring Boot Starter 1.3.1
原文:https://stackoverflow.com/questions/50283388
最满意答案
请尝试使用以下代码段。
<script> function checkLevelZone(sender, args) { var reVal = false; var retValCB = false; var l_level = $find("<%=ActOverviewLevel.ClientID %>"); var l_levelInd = l_level.get_selectedIndex(); var l_zone = $find("<%=ActOverViewZone.ClientID %>"); var l_zoneInd = l_zone.get_selectedIndex(); if (l_zoneInd > 0 && l_levelInd > 0) { $("#" + "<%=cbOffSite.ClientID %>").attr("checked", "checked"); $("#" + "<%=txtOffSite.ClientID %>").attr("disabled", "disabled"); } else { $("#" + "<%=cbOffSite.ClientID %>").removeAttr("checked"); $("#" + "<%=txtOffSite.ClientID %>").removeAttr("disabled"); } return reVal; } </script>
如果有任何疑虑,请告诉我。
Please try with the below code snippet.
<script> function checkLevelZone(sender, args) { var reVal = false; var retValCB = false; var l_level = $find("<%=ActOverviewLevel.ClientID %>"); var l_levelInd = l_level.get_selectedIndex(); var l_zone = $find("<%=ActOverViewZone.ClientID %>"); var l_zoneInd = l_zone.get_selectedIndex(); if (l_zoneInd > 0 && l_levelInd > 0) { $("#" + "<%=cbOffSite.ClientID %>").attr("checked", "checked"); $("#" + "<%=txtOffSite.ClientID %>").attr("disabled", "disabled"); } else { $("#" + "<%=cbOffSite.ClientID %>").removeAttr("checked"); $("#" + "<%=txtOffSite.ClientID %>").removeAttr("disabled"); } return reVal; } </script>
Let me know if any concern.
相关问答
更多-
您可以使用从这篇文章改编的typeOf函数修复与此函数结合的JavaScript typeof运算符 : function typeOf( obj ) { return ({}).toString.call( obj ).match(/\s(\w+)/)[1].toLowerCase(); } function checkTypes( args, types ) { args = [].slice.call( args ); for ( var i = 0; i < types.length; ...
-
javascript检查函数并提供输出(如果存在)(javascript checking for function and giving output if it exists)[2023-06-26]
您需要小心检查可能尚未在JavaScript中定义的名称。 引用未定义的名称将产生错误: > if (Examplefunction) console.log('exists'); else console.log('???') ReferenceError: Examplefunction is not defined 但是,使用typeof检查名称是安全的,无论该名称是否已定义。 因此,要检查变量是否已定义为真值,您应该使用: if (typeof Examplefunction != 'undefi ... -
有什么不对的 那么你正在使用jQuery检查是否存在jQuery。 如果没有加载jQuery,那么$()根本就不会运行,并且你的回调函数将不会执行,除非你使用另一个库,而且这个库可以共享相同的$()语法。 删除你的$(document).ready() (使用像window.onload这样的东西): window.onload = function() { if (window.jQuery) { // jQuery is loaded alert("Yea ...
-
使用indexOf()而不是比较src 例如 var img_el = document.getElementById("on_off_img"); if ( img_el.src.indexOf('on.png') > -1) { img_el.src = 'img/off.png' } else { img_el.src = 'img/on.png' } use indexOf() instead of comparing the src e.g var img_el = document. ...
-
Javascript:在调用之前检查兄弟帧中是否存在函数(Javascript: Checking if function exists in sibling frame before calling)[2023-08-17]
你的代码肯定会有效。 看来myfunction不在子窗口的全局范围内。 当您引用parent.frames['mainframe']您实际上是在“大型机”iframe中引用window对象。 这是一个JSFiddle,其中子函数在window对象上显式设置,并且工作得很好。 JSFiddle示例 Your code should definitely work. It does appear that myfunction isn't in the global scope on the child win ... -
为什么有必要return object ? 没有必要执行object && ,而是一个虚假值的快捷方式,可以避免方法调用null等值。 当然,如果你正在寻找速度,你可能应该选择typeof object == "function" 为什么像Object.prototype.toString.call(myFunc)这样的东西不够用? 这就足够了。 why is it necessary to return object? It's not necessary to do object &&, but a s ...
-
我建议你创建一个JSON.stringify(o)的MD5哈希值,并将其存储在你的hashmap中,并引用你的存储对象作为哈希的数据。 为了确保JSON.stringify()中没有对象键顺序差异,您必须创建对键进行排序的对象的副本。 然后,当每个新对象进入时,您将根据哈希映射进行检查。 如果在哈希映射中找到匹配项,则将传入对象与您存储的实际对象进行比较,以查看它们是否真正重复(因为可能存在MD5哈希冲突)。 这样,您就拥有了一个可管理的哈希表(其中只有MD5哈希值)。 这是用于创建对象的规范字符串表示的代 ...
-
Javascript函数检查(Javascript Function Checking)[2023-09-10]
请尝试使用以下代码段。