我有一个使用myBatis进行持久化的项目.下面的方法"A"工作正常,直到我添加了一些外键并将我的表从myISAM转换为innoDB.转换后,方法"A"将无声地失败,甚至在日志中都没有警告.转换后,只有方法"B"才能成功插入.两种方法都将正确的sql写入日志,但只有"B"才有效.
任何人都可以告诉我为什么我现在需要提交,但以前没有必要提交?
//doesnt work, but worked previously
public void A(Role role) {
SqlSession session = sqlSessionFactory.openSession();
try {
RoleMapper mapper = session.getMapper(RoleMapper.class);
mapper.updateByPrimaryKeySelective(role);
}catch(Exception e){
logger.error(e);
} finally {
session.close();
}
return;
}
//works correctly, but why?
public void B(Role role) {
SqlSession session = sqlSessionFactory.openSession();
try {
RoleMapper mapper = session.getMapper(RoleMapper.class);
mapper.updateByPrimaryKeySelective(role);
session.commit();
}catch(Exception e){
logger.error(e);
} finally {
session.close();
}
return;
}
Run Code Online (Sandbox Code Playgroud) 也许我遗漏了一些明显的东西,但我无法获得一个简单的映射插入语句成功执行.
使用以下界面
public interface CustomItemMapper
{
Integer insert(CustomItem item, @Param("extra") String someparam);
}
Run Code Online (Sandbox Code Playgroud)
以及以下XML映射
<insert id="insert" useGeneratedKeys="false" parameterType="map" keyProperty="id">
insert into CustomItem (id, column2, column3, column4, column5, column6)
values (#{id}, #{field2}, #{field3}, #{field4}, #{field5}, #{extra})
</insert>
Run Code Online (Sandbox Code Playgroud)
和这段代码
SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) servletContext.getAttribute("SqlSessionFactory");
SqlSession session = sqlSessionFactory.openSession();
try
{
CustomItemMapper mapper = session.getMapper(CustomItemMapper.class);
mapper.update(item);
session.commit();
}
finally
{
session.close();
}
Run Code Online (Sandbox Code Playgroud)
我得到以下调试输出:
Checked out connection 368716606 from pool.
ooo Connection Opened
==> Executing: insert into CustomItem (id, column2, column3, column4, column5, column6) …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以将两个或更多参数传递给mybatis SqlSession插入方法,而无需为此创建新类.我知道resultType ="hashmap"可以用来从select中返回数据,但是如何传递数据呢?还想知道为什么不为多个参数使用varargs插入方法.
在此先感谢Remis B.
我试图使用mybatis运行一个简单的SQL查询但它给我以下异常
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.mycom.myproject.db.mybatis.dao.UserMapper.countByExample
org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:660)
org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:495)
org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:488)
org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:236)
org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:71)
org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:39)
$Proxy9.countByExample(Unknown Source)
com.mycom.myproject.controller.LoginController.login(LoginController.java:39)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
Run Code Online (Sandbox Code Playgroud)
我的UserMapper.xml是
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mycom.myproject.db.mybatis.dao.UserMapper" >
<select id="countByExample" resultType="int">
select count(*) from users
</select>
</mapper>
Run Code Online (Sandbox Code Playgroud)
我的UserMapper是
public interface UserMapper {
int countByExample();
}
Run Code Online (Sandbox Code Playgroud)
我试图在我的LoginController中访问它
public class LoginController
{
static final Logger logger = …Run Code Online (Sandbox Code Playgroud) 我已经为酒店管理员的模拟创建了一个登录页面.现在我想为它添加会话时间功能.换句话说,假设用户离开计算机(他仍然登录管理网页)大约10分钟左右.然后,当他回来时,我想结束当前会话,然后重定向到登录页面(这更安全,他的个人信息永远不会丢失!).
我该如何做到这一点?
public class LoginServlet extends SpringInjectedServlet {
@Autowired
private LoginService loginService;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String id = req.getParameter("id");
String password = req.getParameter("password");
String error = null;
//code for checking correct input
}
// mock
private boolean check(String id, String password) {
return loginService.authenticate(id, password);
}
@Override
public void init() throws ServletException {
System.out.println("LoginServlet");
}
}
Run Code Online (Sandbox Code Playgroud) 我刚刚安装了Mybatis Migration并按照他们的说明操作.init,new,status命令有效,但"向上迁移"会导致以下错误:
------------------------------------------------------------------------
-- MyBatis Migrations - up
------------------------------------------------------------------------
========== Applying: 20140410190604_create_changelog.sql =======================
Error executing: -- Create Changelog
-- Default DDL for changelog table that will keep
-- a record of the migrations that have been run.
-- You can modify this to suit your database before
-- running your first migration.
-- Be sure that ID and DESCRIPTION fields exist in
-- BigInteger and String compatible fields respectively.
CREATE TABLE CHANGELOG (
ID NUMERIC(20,0) NOT …Run Code Online (Sandbox Code Playgroud) 处理使用的Web项目postgresql 9.3 postgis 2.1.
表中有一列类型geography,它只存储一个point.
现在我需要通过JDBC使用Java对象插入/选择类型.
阅读postgis手册后,没有找到太多相关信息.
问题是:
mybatis使用,那么它是否会影响上述问题的答案?我在我的XML文件中定义了一个大的搜索查询
<select id="searchItems" resultMap="myMap" parameterType="map">
SELECT
a lot of field
FROM multiple table with few joins
WHERE with few conditions
LIMIT x,y
</select>
Run Code Online (Sandbox Code Playgroud)
上面的查询使用limit来返回分页结果并避免在搜索中返回整个项目.但是对于一个要求,我还需要返回查询找到的项目总数.
我的问题是:我如何重用上面的查询而只选择count(*),显然没有LIMIT?有没有办法分隔每个查询部分并在<select>标记中重用它们?
我想检查我的输入字符串是否有潜在的SQL注入.
这是我的类,方法和查询:
public class UserNamesQuery {
public static String getUserNames(Map<String, Object> params) {
String userNames = (String) params.get("userNames");
return "SELECT * FROM users WHERE name IN (" + userNames + ") ";
}
}
Run Code Online (Sandbox Code Playgroud)
是否有工具或快速方法来验证userNames没有SQL注入?
请注意,我使用的是Mybatis
如何读取属性文件中的系统环境变量.我正在使用MyBatis maven插件进行数据库迁移.MyBatis使用基于环境的属性文件.我试图在属性文件中读取环境变量,如:
development.properties
username=${env.username}
password=${env.password}
Error: FATAL: role "${env.username}" does not exist
Run Code Online (Sandbox Code Playgroud)
我将用户名和密码存储在mac上的".profile"文件中.读取这些变量的正确方法是什么?
mybatis ×10
java ×6
ibatis ×2
mysql ×2
spring ×2
code-reuse ×1
count ×1
duplicates ×1
jdbc ×1
jsp ×1
mapping ×1
maven ×1
persistence ×1
postgis ×1
postgresql ×1
servlets ×1
spring-mvc ×1
sql ×1