前面学习了MyBatis和Spring两个框架,MyBatis用于数据库连接,Spring用于解决企业应用开发的复杂性,现在把MyBatis纳入Spring中,整合搭建登录功能项目
构建web项目–>在web.WEB-INF下构建lib库,导入开发Spring需要的jar包
我把需要的jar包放在下面:
https://download.csdn.net/download/qq_43171656/12650800
其中Spring和MyBatis整合需要的jar包如下:
mybatis-spring-1.2.3.jar (mybatis与spring的整合包)
spring-jdbc-4.1.6.RELEASE.jar
依赖于:
spring-aop-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar 事务
首先在src目录下引入数据库连接配置文件db.propertries和Spring的全局配置文件applicationContext-service.xml
再根据MVC架构大致搭建
com.spring.mapper:数据访问层(持久化层)——数据的增删改查,与数据库建立连接;封装了对数据库的curd操作
com.spring.pojo:简单的Java对象
com.spring.service:业务逻辑层——做一些业务逻辑地处理,并给控制层返回结果
com.spring.view:视图层
大致结构如下:
在applicationContext-service.xml中引入约束:
<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" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
beans>
使用注解注入的方式扫描扫描service.impl下面所有的类
<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" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.spring.service.impl">context:component-scan>
beans>
连接数据库配置,底层使用的是Spring自带的事务管理机制:
<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" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.spring.service.impl">context:component-scan>
<context:property-placeholder location="db.properties">context:property-placeholder>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.spring.mapper">property>
bean>
beans>
db.properties:
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=servlet
jdbc.password=servlet
可见,Spring底层用bean都实现了org.springframework.jdbc.datasource.DriverManagerDataSource
驱动类,用于连接数据库。
org.mybatis.spring.SqlSessionFactoryBean工厂类,用于生成SqlSession实例的工厂,还可以看到在xml中把dataSource注入到了工厂类当中,连接数据库
org.mybatis.spring.mapper.MapperScannerConfigurer mapper配置类
把mapper下面所有的类都交给Spring管理
com.spring.mapper.UserMapper:
package com.spring.mapper;
import com.spring.pojo.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
@Repository //dao层的接口交给Spring管理声明
public interface UserMapper {
@Select("select * from t_user where uname = #{uname} and pwd = #{pwd}")
public User select(User user);
}
com.spring.pojo.User:
package com.spring.pojo;
import java.io.Serializable;
public class User implements Serializable {
private int tid;
private String uname;
private String pwd;
private int sex;
public User() {
}
public User(int tid, String uname, String pwd, int sex) {
this.tid = tid;
this.uname = uname;
this.pwd = pwd;
this.sex = sex;
}
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
@Override
public String toString() {
return "User{" +
"tid=" + tid +
", uname='" + uname + '\'' +
", pwd='" + pwd + '\'' +
", sex=" + sex +
'}';
}
}
com.spring.service.impl.UserServiceImpl:
package com.spring.service.impl;
import com.spring.mapper.UserMapper;
import com.spring.pojo.User;
import com.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired //自动按照类型注入
private UserMapper mapper;
@Override
public User select(User user) {
return mapper.select(user);
}
}
com.spring.service.UserService:
package com.spring.service;
import com.spring.pojo.User;
public interface UserService {
public User select(User user);
}
com.spring.view.TestSpring:
package com.spring.view;
import com.spring.pojo.User;
import com.spring.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
@Autowired
private UserServiceImpl usi;
public TestSpring(){
//导入配置
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
usi = (UserServiceImpl)ac.getBean("userServiceImpl");
}
@Test
public void select(){
User user = new User();
user.setUname("王五");
user.setPwd("123456");
User u = usi.select(user);
System.out.println(u);
}
}