spring、mybatis 配置方式详解(常用两种方式)

网友投稿 362 2023-03-06


spring、mybatis 配置方式详解(常用两种方式)

在之前的文章中总结了三种方式,但是有两种是注解sql的,这种方式比较混乱所以大家不怎么使用,下面总结一下常用的两种总结方式:

一、 动态代理实现 不用写dao的实现类

这种方式比较简单,不用实现dao层,只需要定义接口就可以了,这里只是为了记录配置文件所以程序写的很简单:

1、整体结构图:

2、三个配置文件以及一个映射文件

(1)、程序入口以及前端控制器配置 web.xml

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"

version="3.0">

website1

<!-- 设置监听,在web容器启动时自动装配ApplicationContext的配置信息-->

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:config/springmvc-servlet.xml,

classpath:config/ApplicationContext.xml

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

forceEncoding

true

encodingFilter

*.do

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:config/springmvc-servlet.xml

1

springmvc

*.do

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"

version="3.0">

website1

<!-- 设置监听,在web容器启动时自动装配ApplicationContext的配置信息-->

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:config/springmvc-servlet.xml,

classpath:config/ApplicationContext.xml

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

forceEncoding

true

encodingFilter

*.do

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:config/springmvc-servlet.xml

1

springmvc

*.do

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

(2)、扫描控制层、自动注入以及视图解析器的配置 springmvc-servlet.xml

xmlns:aop="http://springframework.org/schema/aop" xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:util="http://springframework.org/schema/util"

xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.1.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-3.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.1.xsd

http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-3.1.xsd">

xmlns:aop="http://springframework.org/schema/aop" xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:util="http://springframework.org/schema/util"

xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.1.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-3.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.1.xsd

http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-3.1.xsd">

(3)、数据源、service 自动扫描注入、spring代管mybatissqlsessionFactory 、dao层接口动态代理以及事务的配置ApplicationContext.xml

这里会有多中配置文件

1)、单数据源,动态代理实现dao层接口时不设置sqlSessionFactoryBeanName、或sqlSessionTemplateBeanName 两个属性的值

xmlns:tx="http://springframework.org/schema/tx" xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.2.xsd">

${jdbc.driverClassName}

${jdbc.url}

${jdbc.username}

${jdbc.password}

xmlns:tx="http://springframework.org/schema/tx" xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.2.xsd">

${jdbc.driverClassName}

${jdbc.url}

${jdbc.username}

${jdbc.password}

2)、单数据源配置 sqlSessionFactoryBeanName 这个属性值

xmlns:tx="http://springframework.org/schema/tx" xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.2.xsd">

${jdbc.driverClassName}

${jdbc.url}

${jdbc.username}

${jdbc.password}

xmlns:tx="http://springframework.org/schema/tx" xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.2.xsd">

${jdbc.driverClassName}

${jdbc.url}

${jdbc.username}

${jdbc.password}

3)、单数据源配置sqlSessionTemplateBeanName 这个属性值

xmlns:tx="http://springframework.org/schema/tx" xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.2.xsd">

${jdbc.driverClassName}

${jdbc.url}

${jdbc.username}

${jdbc.password}

xmlns:tx="http://springframework.org/schema/tx" xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.2.xsd">

${jdbc.driverClassName}

${jdbc.url}

${jdbc.username}

${jdbc.password}

4)、多数据源

注意如果是多数据源则一定要使用sqlSessionFactoryBeanName 或sqlSessionTemplateBeanName 来指定具体的数据源,不知道在上面的配置中有没有注意到,如果使用sqlSessionTemplateBeanName 的话要

来创建具体的实例并赋值给sqlSessionTemplateBeanName 这个属性。

(4)、mybatis SQL映射文件 userMapper.xml:

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

insert into user(id,name) values(#{id},#{name})

ok  到这里配置文件到搞定了下面来看看控制层,业务逻辑层以及dao层的代码。

3、controller层

package com.website.controller;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import com.website.service.UserService;

@Controller

@RequestMapping(value = "/user")

public class UserController {

// 注入userService 对象

@Autowired

private UserService userService;

@RequestMapping(value = "/save.do", method = RequestMethod.GET)

public String saveUser(HttpServletRequest request,

HttpServletResponse response) {

String id = request.getParameter("id");

String name = request.getParameter("name");

Map map = new HashMap();

map.put("id", id);

map.put("name", name);

userService.saveUser(map);

return "index";

}

}

4、service层

package com.website.service;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.website.dao.UserDao;

@Service("userService")

@Transactional

public class UserService {

// 注入dao接口实现类实例

// @Resource、@Autowired两种注入方式都可以

@Autowired

private UserDao userDao;

public void saveUser(Map map) {

int end = userDao.saveUser(map);

System.out.println("end:" + end);

}

}

5、dao 层 接口

package com.website.dao;

import java.util.Map;

//com.website.dao.UserDao

public interface UserDao {

int saveUser(Map map);

}

dao 接口的完整路劲就是这个dao 接口对应的那个映射文件的namespace 而方法名就是 id的值

ok到这里这种配置方式都完了,也有了一个完整的小demo,下面我们简单总结一下:

这种配置方式相比之前的配置方式(下面也会写出来)特别之处就是他使用了dao层接口的动态代理方式实现了,之前我们会在dao层自己手动实现dao层然后自动注入SqlSessionTemplate 实例来调用具体的方法 比如 insert("","")  selectOne("","") 等方法 其中第一个参数就是映射文件的地址: namespace+id  而第二个参数就是传递的条件这样mybatis 就会按照我们传递的这两个参数找到具体的映射文件进行解析查询。而这里使用动态代理就省去了我们实现dao接口的这一步骤,而是由spring提我们实现了,那有个问题,查询条件参数我们传递了,但映射文件的具体路径即:namespce+id  没有传递怎么办,那就是你的映射文件的namespace 必须是接口的类全名称而id 必须是接口中的方法名称,这样动态代理就能找到路劲了也有了参数了。 这样一来是不是觉得就一样了啊哈哈哈!

二、手动实现dao层接口

下面先来看看手动实现dao层的配置以及代码:

1、正题结构图

2、三个配置文件以及映射文件

(1)、程序入口,前端控制器配置 web.xml

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">

website2

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:config/springmvc-servlet.xml,

classpath:config/ApplicationContext.xml

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

forceEncoding

true

encodingFilter

*.do

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:config/springmvc-servlet.xml

1

springmvc

*.do

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">

website2

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:config/springmvc-servlet.xml,

classpath:config/ApplicationContext.xml

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

forceEncoding

true

encodingFilter

*.do

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:config/springmvc-servlet.xml

1

springmvc

*.do

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

(2)、扫描控制层、自动注入以及视图解析器的配置 springmvc-servlet.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:util="http://springframework.org/schema/util"

xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.1.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-3.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.1.xsd

http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-3.1.xsd">

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc" xmlns:util="http://springframework.org/schema/util"

xmlns:tx="http://springframework.org/schema/tx" xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.1.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.1.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-3.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.1.xsd

http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-3.1.xsd">

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

(3)、数据源、service 自动扫描注入、spring代管mybatissqlsessionFactory 以及事务的配置ApplicationContext.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:tx="http://springframework.org/schema/tx"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.2.xsd">

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

${jdbc.driverClassName}

${jdbc.url}

${jdbc.username}

${jdbc.password}

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:tx="http://springframework.org/schema/tx"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.2.xsd">

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

${jdbc.driverClassName}

${jdbc.url}

${jdbc.username}

${jdbc.password}

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

${jdbc.driverClassName}

${jdbc.url}

${jdbc.username}

${jdbc.password}

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

(4)、mybatis 映射文件

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

insert into user(id,name,password) values(#{id},#{name},#{password})

你可能看到了这里的映射文件的namespace +id  是自定义的而不是dao 层接口的全类名+id

3、控制层Controller

package com.website.controller;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import com.website.service.UserService;

/**

* @author WHD data 2016年6月5日

*/

@Controller

@RequestMapping(value = "/user")

public class UserController {

@Autowired

private UserService userService;

@RequestMapping(value = "/save.do")

public String saveUser(HttpServletRequest request,

HttpServletResponse response) {

String id = request.getParameter("id");

String name = request.getParameter("name");

String password = request.getParameter("password");

Map map = new HashMap();

map.put("id", id);

map.put("name", name);

map.put("password", password);

userService.saveUser(map);

return "index";

}

}

4、业务逻辑层 service

package com.website.service;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.website.dao.UserDao;

/**

* @author WHD data 2016年6月5日

*/

@Service("userService")

@Transactional

public class UserService {

@Autowired

private UserDao userDao;

public void saveUser(Map map) {

userDao.saveUser(map);

}

}

5、dao层

package com.website.dao;

import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;

/**

* @author WHD data 2016年6月5日

*/

@Repository("userDao")

public class UserDao {

@Autowired

private SqlSessionTemplate sqlSession;

public void saveUser(Map map) {

int end = sqlSession.insert("com.website.userMapper.insertUser", map);

System.out.println("end" + end);

}

}

我们看倒dao层的 SqlSessionTemplate  这个其实是mybatis中的SqlSession 对象,我们看到在ApplicationContext.xml 中配置了,所以在我们使用时spring会帮我们自动注入,我们直接使用就可以了不用去自己创建,这也就是所谓的控制反转。ok 到此两种文件的配置方式就结束了。

总结

以上所述是给大家介绍的spring、mybatis 配置方式详解(常用两种方式),希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java函数习惯用法详解
下一篇:用户api管理(api user)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~