我想配置與彈簧3的MyBatis我下面的文章在MyBatis的彈簧3不工作
http://www.raistudies.com/spring/integrating-mybatis-3-and-spring-frameworks-part-2/
我使用的MyBatis生成器生成所有的映射器和模型calsses。使用mybatis發電機後,我有以下課程。
import java.util.Date;
public class User extends BaseModel {
/**
* This field was generated by MyBatis Generator. This field corresponds to the database column users.id
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
private Integer id;
/**
* This field was generated by MyBatis Generator. This field corresponds to the database column users.username
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
private String username;
/**
* This field was generated by MyBatis Generator. This field corresponds to the database column users.email
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
private String email;
/**
* This field was generated by MyBatis Generator. This field corresponds to the database column users.crypted_password
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
to the database column users.first_name
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
private String firstName;
/**
* This field was generated by MyBatis Generator. This field corresponds to the database column users.last_name
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
private String lastName;
// I renoved the getter setter to save space.
}
它還創建一個UserExample.java。我不明白爲什麼這個文件已經創建。我只是應付這個班的一部分。
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class UserExample {
/**
* This field was generated by MyBatis Generator. This field corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
protected String orderByClause;
/**
* This field was generated by MyBatis Generator. This field corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
protected boolean distinct;
/**
* This field was generated by MyBatis Generator. This field corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
protected List<Criteria> oredCriteria;
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
public UserExample() {
oredCriteria = new ArrayList<Criteria>();
}
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
public String getOrderByClause() {
return orderByClause;
}
然後它創建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">
<resultMap id="BaseResultMap" type="com.mycom.myproject.db.mybatis.model.User">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Aug 03 09:28:44 BST 2012.
-->
<result column="id" jdbcType="INTEGER" property="id" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="first_name" jdbcType="VARCHAR" property="firstName" />
<result column="last_name" jdbcType="VARCHAR" property="lastName" />
</resultMap>
<sql id="Example_Where_Clause">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Aug 03 09:28:44 BST 2012.
-->
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Aug 03 09:28:44 BST 2012.
-->
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Aug 03 09:28:44 BST 2012.
-->
id, username, email, crypted_password, password_salt, persistence_token, login_count,
failed_login_count, last_request_at, current_login_at, last_login_at, current_login_ip,
last_login_ip, created_at, updated_at, creator_id, updater_id, first_name, last_name
</sql>
<select id="selectByExample" parameterType="com.mycom.myproject.db.mybatis.model.UserExample" resultMap="BaseResultMap">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Aug 03 09:28:44 BST 2012.
-->
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from users
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
它還創建UserMapper界面,我認爲可以用注射
import com.mycom.myproject.db.mybatis.model.User;
import com.mycom.myproject.db.mybatis.model.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
int countByExample(UserExample example);
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
int deleteByExample(UserExample example);
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
int insert(User record);
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
int insertSelective(User record);
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
List<User> selectByExample(UserExample example);
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
int updateByExampleSelective(@Param("record") User record,
@Param("example") UserExample example);
/**
* This method was generated by MyBatis Generator. This method corresponds to the database table users
* @mbggenerated Fri Aug 03 09:28:44 BST 2012
*/
int updateByExample(@Param("record") User record,
@Param("example") UserExample example);
}
我的彈簧servlet.xml文件是
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<context:component-scan
base-package="com.mycom.myproject" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/mydatabase"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource" />
</bean>
<!-- scan for mappers and will automatically scan the whole classpath for xmls -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="basePackage" value="com.mycom.myproject.db.mybatis.dao" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
現在我quetsion是如果我願意,我可以如何獲得用戶列表或簡單的用戶帳戶。我在mysql數據庫的用戶表中有許多用戶。
我寫了一個樣本控制器類
@Controller
@RequestMapping("/")
public class LoginController
{
static final Logger logger = Logger.getLogger(LoginController.class);
private UserMapper userMapper = null;
@RequestMapping("/login")
public ModelAndView login(@ModelAttribute User userBean, Model model){
return new ModelAndView("login", "userBean", userBean);
}
@Autowired
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
}
,但它給了我下面的錯誤。
Error creating bean with name 'loginController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.mycom.myproject.controller.LoginController.setUserMapper(com.mycom.myproject.db.mybatis.dao.UserMapper); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mycom.myproject.db.mybatis.dao.UserMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
我要說,同樣的問題:http://stackoverflow.com/q/11781202/980472 – jddsantaella 2012-08-03 12:00:03