Java环境中MyBatis与Spring或Spring MVC框架的集成方法
Java环境中MyBatis与Spring或Spring MVC框架的集成方法
与Spring3集成
Spring作为基础框架,可以集成后端框架,如Hibernate,MyBatis等。
前面是介绍单独使用MyBatis的,大致逻辑是:
sqlSessionFactory <- configuration file (包括数据库连接配置)
IXxxMapper <- sqlSession <- sqlSessionFactory
<- mapper interface <- mapper xml
得到IxxMapper后,就可以调用其方法进行数据交互了。
和Spring集成时,上面的这些对象需要作为bean来管理:
dataSource bean <- 数据库连接配置
sqlSessionFactory bean <- dataSource
<- configuration file
userMapper bean <- sqlSessionFactory
<- mapper interface
1. 在pom.xml中加入依赖:
2. 在类路径下创建beans-da.xml文件:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">
3. 测试类:
@ContextConfiguration(locations = { "classpatJCzEidXAh:beans-da.xml" })
public class SpringIntegrationTest extends AbstractTestNGSpringContextTests {
private static final Logger log = LoggerFactory.getLogger(SpringIntegrationTest.class);
@Resource
IUserMapper mapper;
@Test
public void queryTest() {
User user = mapper.getUserById(1);
log.info("Name: {}, address: {}", user.getName(), user.getAddress());
}
}
与SpringMVC集成
这里我们建立在与Spring3集成基础上来讲:
1. 往pom.xml添加SpringMVC和Freemarker依赖:
2. 在web.xml中加入Spring的监听器和SpringMVC的servlet:
3. 在WEB-INF下新建:
Spring配置文件applicationContext.xml:
xmlns:p="http://springframework.org/schema/p" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xsi:schemaLocation=JCzEidXA"http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd"> p:driverClassName="${driverClassName}" p:url="${url}" p:username="${user_name}" p:password="${password}" />
xmlns:p="http://springframework.org/schema/p"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xsi:schemaLocation=JCzEidXA"http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">
p:driverClassName="${driverClassName}" p:url="${url}" p:username="${user_name}" p:password="${password}" />
p:driverClassName="${driverClassName}"
p:url="${url}"
p:username="${user_name}"
p:password="${password}" />
类路径下的database.properties:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
user_name=root
password=123456
注:因为MapperScannerConfigurer可能会导致username取的是系统用户的账号,而造成数据库连接失败,所以改成其它值:user_name。
SpringMVC配置文件hbatis-servlet.xml:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc.xsd">
4. MVC:
控制层:UserController.java
@Controller
@RequestMapping("/article")
public class UserController {
@Autowired
IUserMapper mapper;
@RequestMapping("/list")
public String showAll(ModelMap modelMap) {
List
modelMap.addAttribute("articles", articles);
return "main.ftl";
}
}
视图层:main.ftl:
<#list articles as article>
#list>
5. 启动工程,浏览器输入:http://localhost:8080/hbatis/article/list.htm查看结果。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~