我有一个带有Foos的MySQL表.每个Foo都有一个数字非唯一代码和一个名称.现在我需要查找是否有任何具有某些代码的Foo恰好具有以给定字符串开头的名称.在普通的SQL中,这将是微不足道的:
select * from FOO where CODE in (2,3,5) and NAME like 'bar%';
Run Code Online (Sandbox Code Playgroud)
但是我现在如何在Spring中正确地做到这一点?如果不需要'like'运算符,我会这样做:
public List<Foo> getByName(List<Integer> codes, String namePart) {
String sql = "select * from FOO where CODE in (:codes) and NAME=:name"
Map<String,Object> params = new HashMap<String,Object>();
params.put("codes", codes);
params.put("name", namePart);
return getSimpleJdbcTemplate().query(sql, new FooRowMapper(), params);
}
Run Code Online (Sandbox Code Playgroud)
然而,与"喜欢"似乎没有任何工作:NAME like :name%,NAME like ':name%'或NAME like ?%使用占位符代替命名参数时.
我可能是残酷的并且输入它
String sql = "select * from FOO where CODE in (:codes) and NAME like '"+namePart+"%'";`
Run Code Online (Sandbox Code Playgroud)
但是很明显,如果Spring能够正确地清理输入参数等,那就太好了,你知道......
你会认为Spring会以某种方式支持它,但我无法弄明白.
背景:所以,我有几个bean连接外部系统.对于开发,模拟外部系统并将接口bean替换为产生或多或少静态响应的一些实现是很方便的.所以我一直在做的是创建一个接口,真正的实现和这样的存根实现:
public interface ExternalService {
// ...
}
@Service
public class ExternalServiceImpl implements ExternalService {
// ...
}
@Service
@Primary
@Profile({"stub"})
public class StubExternalService implements ExternalService {
// ...
}
Run Code Online (Sandbox Code Playgroud)
...这很有效:如果存根配置文件不存在,则根本不会加载存根bean.如果它存在,它很好地取代了真正的实现,因为@Primary注释.
问题:然而,现在,我已经第一次运行了我实际上有两个相同接口的实际实现的情况.其中一个被定义为主要,但另一个也可以通过从应用程序上下文加载它来使用.
我仍然想创建一个存根服务来替换它们,但这次我将存根定义为@Primary的旧方法不起作用,因为已经有一个主要实现.基本上我需要的是在设置存根配置文件时不加载主bean的方法,但我不知道如何做到这一点.Web搜索或其他Stack Overflow问题似乎没有帮助.
我正在尝试使用REST Assured来检查我的服务器返回的HTML文档的某些属性.一个SSCCE证明问题将如下所示:
import static com.jayway.restassured.path.xml.config.XmlPathConfig.xmlPathConfig;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import com.jayway.restassured.path.xml.XmlPath;
public class HtmlDocumentTest {
@Test
public void titleShouldBeHelloWorld() {
final XmlPath xml = new XmlPath("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
+ "<head><title>Hello world</title></head><body></body></html>")
.using(xmlPathConfig().with().feature("http://apache.org/xml/features/disallow-doctype-decl", false));
assertThat(xml.get("//title[text()]"), is("Hello world"));
}
}
Run Code Online (Sandbox Code Playgroud)
现在,这个尝试结束于大约30秒左右的com.jayway.restassured.path.xml.exception.XmlPathException: Failed to parse the XML document所有可能的错误导致java.net.ConnectException: Connection timed out!
如果我删除该行,xmlPathConfig().with().feature(...)测试失败,因为DOCTYPE is disallowed when the feature "http://apache.org/xml/features/disallow-doctype-decl" set to …
我正在尝试构建一个JavaScript程序来查询给定播放列表的YouTube API,按持续时间排序.一切都工作得很完美,但订购并不考虑整个播放列表,只有25个最新的视频!这是一个JSFiddle的最小完整工作示例,这里是它的JavaScript部分:
var playlistId = "UUAuUUnT6oDeKwE6v1NGQxug";
jQuery.getJSON(
"https://gdata.youtube.com/feeds/api/playlists/"+playlistId+"?v=2&orderby=duration&alt=json",
function(data) {
$.each(data.feed.entry, function(key, val) {
var title = val.title.$t;
var url = val.content.src;
var duration = val.media$group.yt$duration.seconds;
var minutes = Math.floor(duration / 60);
var seconds = (duration % 60);
if( seconds < 10 ) seconds = "0"+seconds;
var newRow = $("<tr></tr>");
newRow.append("<td><a href='"+url+"'>"+title+"</a></td>");
newRow.append("<td class='dur'>"+minutes+":"+seconds+"</td>");
$("#videos").append(newRow);
});
}
);
Run Code Online (Sandbox Code Playgroud)
我在XML和JSON中尝试了这个,除了播放列表搜索之外,我还尝试了其他类型的搜索.将API排序为结果的最新视频似乎毫无意义.我究竟如何检索播放列表的最长或最短视频,或者由给定用户上传的那些视频?
我正在尝试使用 H2 数据库(使用 Flyway 迁移初始化)和 Spring Data 在我自己的项目中复制Hibernate 示例。问题是,当我保存父对象 BookCategory 时,它也会保存子对象(Book 对象),但它不会保存 book_category_id,即“子”对象的“父 id”!
我的 SQL 看起来像这样:
CREATE SEQUENCE SEQ_IDNUMS START WITH 1;
CREATE TABLE book_category (
id int(11) NOT NULL,
name varchar(255) NOT NULL,
PRIMARY KEY (id,name)
);
CREATE TABLE book (
id int(11) NOT NULL,
name varchar(255) DEFAULT NULL,
book_category_id int(11) DEFAULT NULL
);
alter table book add constraint pk_book_id PRIMARY KEY (id);
alter table book add constraint fk_book_bookcategoryid FOREIGN KEY(book_category_id) REFERENCES book_category(id) ON DELETE CASCADE; …Run Code Online (Sandbox Code Playgroud) java ×3
spring ×2
hibernate ×1
javascript ×1
jdbctemplate ×1
rest-assured ×1
spring-bean ×1
spring-data ×1
spring-jdbc ×1
youtube-api ×1