Spring与Mybatis整合环境搭建
本文用的jar包如下:
1、下载jar包
springMVC:
http://www.springsource.org/download/community
mybatis:
http://code.google.com/p/mybatis/wiki/Downloads
准备工作:在MYSQL里新增数据库test,表user
CREATE TABLE user (userid int(4) NOT NULL AUTO_INCREMENT, username varchar(50), password varchar(50), PRIMARY KEY (userid)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、Spring mvc的基本配置
a---> web.xml
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
b---> 配置数据源文件 database.properties
mysql.database.driver=com.mysql.jdbc.Driver
mysql.database.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
mysql.database.user=root
mysql.database.password=******
c---> Spring 核心配置页 applicationContext.xml
<!-- 配置数据库 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:conf/database.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${mysql.database.driver}" />
<property name="url" value="${mysql.database.url}" />
<property name="username" value="${mysql.database.user}" />
<property name="password" value="${mysql.database.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlMapClient" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="configLocation" value="classpath:conf/SqlMapConfig.xml"></property>
</bean>
<!-- 把加载了 配置文件的 sqlMapClient 注入 SqlSessionTemplate模板-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlMapClient" />
</bean>
<!-- 注册Mapper:也可不指定特定mapper,而使用自动扫描包的方式来注册各种Mapper ,配置如下:-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.dao" />
</bean>
(此文没有配事务,主要还是关心Mybatis的一些配置,详细事务下次再另写文章。)
d---> Spring MVC配置 spring-servlet.xml (放WEB-INF下)
<mvc:annotation-driven/>
<!-- 注解模式 -->
<context:component-scan base-package="com">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 映射jsp -->
<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/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
3、Mybatis的一些配置:
a---> 核心配置SqlMapConfig.xml
<configuration>
<!-- 对应一些POJO类 然后可以在mapper中引用Emp 等就等于引用这个类类型-->
<typeAliases>
<typeAlias type="com.test.model.User" alias="User" />
</typeAliases>
<mappers>
<mapper resource="com/test/dao/UserMapper.xml" />
</mappers>
</configuration>
b---> 利用Mybatis工具生成相应的entity,dao,xml等文件,即:
User.java//实体类
UserExample.java//Example对象,Mybatis自身生成的一些基础查询都可以用它查。
UserMapper.java//DAO接口文件
UserMapper.xml//Mybatis增删改查等配置文件
具体的生成方法见博文:http://angelbill3.iteye.com/blog/1696970
具体用法等有空再写博文吧。
4、至此所有配置+环境都可以用了,写下测试类测试下:
a---> 新建测试Service: TestServiceImpl.java
@Service
public class TestServiceImpl implements TestService{
@Resource
private UserMapper userMapper;
public List<User> getUserList(){
UserExample uEx = new UserExample();
uEx.createCriteria().andUsernameEqualTo("111");
List<User> list = userMapper.selectByExample(uEx);
return list;
}
}
//getUserList() 查询出username为‘111’的用户。(前提得自己在数据库里插入这个用户。
b---> Spring Controller类:TestController.java
@Controller
@RequestMapping("test")
public class TestController {
@Resource
private TestService testService;
@RequestMapping(value = "list")
public ModelAndView getUserTest(HttpServletResponse response) throws Exception {
List list = this.testService.getUserList();
System.out.println("listsize:"+list.size());
ModelAndView mav = new ModelAndView();
mav.addObject(list);
mav.setViewName("list");
return mav;
}
}
打印结果:listsize:1
访问:项目名/test/list.html即可进入此TestController的getUserTest()方法,并将得到的list放到结果页,即项目名/webroot/web-inf/views/list.jsp中。
----------------------------------------------
遇到问题:
在用UserMapper文件的时候,报错:
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for ......
网上看到的可能导致此类错异常的原因:
The three names have to match:
- interface = com.enlliance.inventory.mappers.SettingMapper.class
- mapper file = /com/enlliance/inventory/mappers/SettingMapper.xml
- mapper namespace = com.enlliance.inventory.mappers.SettingMapper
解决方法:
将UserMapper.xml中的namespace更改如下:
<mapper namespace="com.faj.dao.UserMapper" >
问题解决。
----------------------------------------------
参考资料:
http://blog.sina.com.cn/s/blog_93b15c4d010145i1.html
http://layznet.iteye.com/blog/1021533
//Mybatis的教程系统文章,很全。
http://legend2011.blog.51cto.com/3018495/980150
----------------------------------------------
相关资料:
SpringMVC 声明式事务学习以及问题解决
http://angelbill3.iteye.com/blog/1896502
使用mybatis-generator自动生成Mybatis相关代码
http://angelbill3.iteye.com/blog/1696970