指定包批量扫描产生Mapper实现类

  • 在Spring配置文件(applicationContext.xml)中配置MapperScannerConfigurer
1
2
3
4
<!--  把生成Mapper的单个实现类的MapperFactoryBean删除 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xdl.mapper"></property>
</bean>

自定义标注控制接口产生实现类

  • 创建标注控制接口MyAnnotation
1
2
3
package com.xdl.annotation;
public @interface MyAnnotation{
}

把applicationContext.xml中的MapperScannerConfigurer增加成员变量

1
2
3
4
5
<!--  把上述组件修改 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xdl.mapper"></property>
<property name="annotationClass" value="com.xdl.annotation.MyAnnotation"></property>
</bean>

把Dao接口加上@MyAnnotation

1
2
3
4
@MyAnnotation
public interface XdlBankAccountDao{
...
}

SqlSessionTemplate批量产生实现类

  • 建立一个项目,导入jar包(mybatis.jar mybatis-spring.jar ojdbc14.jar ioc aop dao 数据库连接池)拷贝Spring配置文件到src

  • 编写实体类

  • 编写SQL定义文件,根据id查询银行账户

  • 根据Mapper映射器规则,编写DAO接口

  • 在Spring配置文件中配置SqlSessionFactoryBean,依赖于dataSource和SQL定义

  • 编写DAO的实现类,实现DAO接口并注入SqlSessionTemplate类型的对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.xdl.mapper;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.xdl.bean.XdlBankAccount;
@Repository("accountDao")
public class XdlBankAccountDAOOracleImp implements XdlBankAccountDAO {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Override
public XdlBankAccount findAccountById(int id) {
return sqlSessionTemplate.selectOne("findAccountById", id);
}
}

这个对象依赖于sqlSessionFactory。利用SqlSessionTemplate对应的API完成操作。注意创建DAO实现类组件,需要组件扫描

1
2
3
4
5
6
7
8
9
10
11
<!--  创建sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/xdl/mapper/*.xml"></property>
</bean>
<!-- SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.xdl"></context:component-scan>

SSM架构搭建

  • 搭建基于标注的MVC,导入jar包(AOP IOC DAO MVC)

在项目的web-inf下的web.xml文件输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<!-- 配置ispatcherServlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
  • 在项目的src下,拷贝applicationContext.xml文件并输入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!-- 开启组件扫描和 标注形式的mvc -->
<context:component-scan base-package="com.xdl" ></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置ViewResolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 放行静态资源 -->
<mvc:default-servlet-handler/>
</beans>

在web-inf下创建login.jsp文件,并输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
</head>
<body>
<form action="login">
账号:<input type="text" name="acc_no" id="acc_no" value="${acc_no}"/> <br>
密码: <input type="password" name="acc_password" id="acc_password" /> <br>
<input type="submit" value="登录"> ${msg}
</form>
<br>
</body>
</html>

在src下创建com.xdl.controller目录,然后在此目录下创建XdlBankAccountController.class文件并输入

1
2
3
4
5
6
7
8
9
10
package com.xdl.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
class XdlBankAccountController {
@RequestMapping("/toLogin")
public String toLogin() {
return "login";
}
}
  • 搭建Mybatis和Spring整合,根据账号和密码完成对银行账户的查询

导入jar包(mybatis 连接池 连接数据库)

在src下创建com.xdl.bean目录,然后在此目录下创建实体类XdlBankAccount文件并输入

在src下创建com.xdl.mapper目录,然后在此目录下创建接口XdlBankAccountDao文件并输入

1
2
3
4
5
6
7
8
package com.xdl.mapper;
import org.apache.ibatis.annotations.Param;
import com.xdl.bean.XdlBankAccount;
public interface XdlBankAccountDAO {
XdlBankAccount findAccountByAccNoAndAccPassword(
@Param("acc_no") String acc_no,
@Param("acc_password")String acc_password);
}

在com.xdl.mapper目录下创建XdlBankAccountMapper.xml文件并输入

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<!-- namespace指定和哪个Mapper映射器接口对应 -->
<mapper namespace="com.xdl.mapper.XdlBankAccountDAO">
<!-- 定义SQL语句 -->
<select id="findAccountByAccNoAndAccPassword"
resultType="com.xdl.bean.XdlBankAccount">
select * from xdl_bank_account_30 where acc_no = #{acc_no} and
acc_password = #{acc_password}
</select>
</mapper>

在src下的applicationContext.xml文件输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!--  连接池对象的配置  -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" >
<value>oracle.jdbc.OracleDriver</value>
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="system"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 创建sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/xdl/mapper/*.xml"></property>
</bean>
<!-- 批量产生Mapper实现类的组件 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xdl.mapper"></property>
</bean>

在src下创建sqlmap-config.xml文件输入

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<typeAlias type="com.xdl.bean.XdlBankAccount" alias="account"/>
</typeAliases>
</configuration>

修改src下的applicationContext.xml文件,然后就可以看到日志里有sql语句和实体类可以用别名

1
2
3
4
5
6
<!--  创建sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/xdl/mapper/*.xml"></property>
<property name="configLocation" value="classpath:sqlmap-config.xml"></property>
</bean>
  • 先包装一个Service对象,里面注入DAO,然后把Service注入给Controller

在src下创建com.xdl.service目录,然后在此目录下创建XdlBankAccountService.class文件并输入

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.xdl.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xdl.bean.XdlBankAccount;
import com.xdl.mapper.XdlBankAccountDAO;
@Service("accountService")
public class XdlBankAccountService {
@Autowired
private XdlBankAccountDAO accountDao;
public XdlBankAccount login(String acc_no,String acc_password) {
return accountDao.findAccountByAccNoAndAccPassword(acc_no, acc_password);
}
}

在com.xdl.controller目录下的XdlBankAccountController.class文件里输入

1
2
@Autowired
private XdlBankAccountService accountService;
  • 在控制器方法中接收页面请求的参数,调用Service方法判断是否登录成功,成功重定向到main.jsp,否则跳转到login.jsp提示登录失败

在com.xdl.controller目录下的XdlBankAccountController.class文件里输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RequestMapping("/toMain")
public String toMain() {
return "main";
}

@RequestMapping("/login")
public String login(String acc_no,String acc_password,
HttpServletRequest request) {
XdlBankAccount account = accountService.login(acc_no, acc_password);
if(account != null) {
request.getSession().setAttribute("account", account);
return "redirect:toMain";
}else {
request.setAttribute("msg", "登录失败");
return "login";
}
}