赞
踩
假设现在有两类人,需要登录,在不同的登录页面下,检测不同的数据库。一种人的信息存放在mysql,另一种存放在sqlserver,我们现在设置两个映射文件,分别存放不同的SQL语句,这很好理解吧,然后在application.properties里存放两个数据库的配置信息,准备实体类entity,准备一个controller,准备两个mapper接口,以及他们的service实现文件。现在重点来了,我们需要什么把他们串起来。你现在在login页面输入了信息,经过ajax传送到后台,后台调用service里的函数,这些函数,分别调用接口的函数来实现登录校验,将校验结果返回页面ajax,根据结果,跳转或者怎样。这就是我们的主要思路。但是我们想一下,谁来决定你在不同的login页面输入不同类人的信息所调用的不同数据库分别是哪个呢??我们将在DataConfig里配置他。
除了默认的,再加下面几个:
- <dependency>
- <groupId>com.microsoft.sqlserver</groupId>
- <artifactId>sqljdbc4</artifactId>
- <version>4.0</version>
- </dependency>
- <dependency>
- <groupId>net.sf.json-lib</groupId>
- <artifactId>json-lib</artifactId>
- <version>2.4</version>
- <classifier>jdk15</classifier>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.22</version>
- </dependency>
这里注意,由于个人的mysql版本问题,驱动版本的选择要十分谨慎,不然会出现很多错误,经过尝试,对于我的mysql而言,5.1.22的驱动可以较好的兼容。
- spring.datasource.test1.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
- spring.datasource.test1.jdbc-url=jdbc:sqlserver://localhost:1433;DatabaseName=ajax
- spring.datasource.test1.username=sherry
- spring.datasource.test1.password=xiaoai
- spring.mvc.view.prefix=/views/
- spring.mvc.view.suffix=.jsp
- spring.datasource.test2.driver-class-name=com.mysql.jdbc.Driver
- spring.datasource.test2.jdbc-url=jdbc:mysql://127.0.0.1:3306/test
- spring.datasource.test2.username=root
- spring.datasource.test2.password=123456
- package com.example.ajaxloginand.Controller1;
-
-
- import com.example.ajaxloginand.Service.ServiceImpl.UserServiceImpl;
- import com.example.ajaxloginand.Service2.ServiceImpl2.UserServiceImpl2;
- import com.example.ajaxloginand.entity.user;
- import net.sf.json.JSONObject;
- 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.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- @Controller
- public class loginController {
- @Autowired
- private UserServiceImpl userService;
- @RequestMapping("/login")
- @ResponseBody
- public Object login(@RequestParam String ObjectUser){
- //user 转换成 json 对象,再转成 java 对象
- JSONObject userObject = JSONObject.fromObject(ObjectUser);
- user userObj= (user)userObject.toBean(userObject, user.class);
- if(userService.ifhaveuser(userObj.getUsername(),userObj.getPassword()))
- return "success";
- else return "fail";
- }
- @RequestMapping("/test")
- public String test(){
- return "login";
- }
- //配置第二个数据源
- @Autowired
- private UserServiceImpl2 userService2;
- @RequestMapping("/login2")
- @ResponseBody
- public Object login2(@RequestParam String ObjectUser){
- //user 转换成 json 对象,再转成 java 对象
- JSONObject userObject = JSONObject.fromObject(ObjectUser);
- user userObj= (user)userObject.toBean(userObject, user.class);
- if(userService2.ifhaveuser(userObj.getUsername(),userObj.getPassword()))
- return "success";
- else return "fail";
- }
- @RequestMapping("/test2")
- public String test2(){
- return "login2";
- }
- }
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.example.ajaxloginand.Service.Mapper1.UserMapper1">
- <select id="check" resultType="com.example.ajaxloginand.entity.user">
- select * from Users where username=#{UserName} and password=#{PassWord}
- </select>
- </mapper>
- package com.example.ajaxloginand.DataConfig;
-
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.mybatis.spring.SqlSessionFactoryBean;
- import org.mybatis.spring.SqlSessionTemplate;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.boot.jdbc.DataSourceBuilder;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
- import org.springframework.jdbc.datasource.DataSourceTransactionManager;
-
- import javax.sql.DataSource;
-
- @Configuration
- @MapperScan(basePackages = {"com.example.ajaxloginand.Service2"},sqlSessionFactoryRef = "test2SqlSessionFactory")
- //这里一定要注意,这个basePackages是你的mapper接口及service所在的包名,而下面的红线所标注的classpath是mapper.xml所在的位置,这个xml是配置文件,处在resources里,他的路径也要格外区分开。
- public class DataByMySQL {
- @Bean(name="test2DataSource")
- //下面的注解作用就是从application.properties中读取以这个字符串开头的那些配置,设置为数据源的配置
- @ConfigurationProperties(prefix ="spring.datasource.test2")
- public DataSource testDataSource(){
- return DataSourceBuilder.create().build();
- }
- @Bean(name="test2SqlSessionFactory")
- public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)throws Exception{
- SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
- bean.setDataSource(dataSource);
- bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper2/test2/*.xml"));
- return bean.getObject();
- }
- @Bean(name = "test2TransactionManager")
- public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource")DataSource dataSource){
- return new DataSourceTransactionManager(dataSource);
- }
- @Bean(name="test2SqlSessionTemplate")
- public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory")SqlSessionFactory sqlSessionFactory){
- return new SqlSessionTemplate(sqlSessionFactory);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。