spring与mybatis整合

spring与mybatis整合
里插入图片描述竟然需要导入如此之多的jar包,哈哈哈,需要的小伙伴们可以私信我哟!加qq:1348550316.
在这里插入图片描述
本节课内容共运用到了7个文件:
(1) mybatis.xml
(2)applicationContext.xml(主要就是进行配置这个文件了)
(3)User.java
(4)UserMapper.java(相当于dao层)
(5)USerMapper.xml(相当于mybatis框架中dao层中java文件的相应的局部配置文件)
(6)UserService.java
(7)UserServiceImpl.java(通过注解声明对象:@Service,通过注解声明属性类型:@Autowired)
(8)CaipinServlet.java(一个模拟的servlet)

(1) mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- 配置数据库连接环境:driver、url、username、password -->
	<environments default="mysql">
	</environments>
	

</configuration>

(2)applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" 
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd">
       
       <!-- 配置包扫描 -->
       <context:component-scan base-package="cn.java.servlet,cn.java.service.impl" />
       
       <!-- ctrl+shift+H 配置数据源:driver、url、username、password -->
       <bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
       		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
       		<property name="url" value="jdbc:mysql:///dt55"></property>
       		<property name="username" value="root"></property>
       		<property name="password" value="root"></property>
       </bean>
       
       <!-- 扫描局部配置文件 -->
       <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
       		<!-- 告诉spring容器创建SqlSession对象时,连接的是哪个数据库 -->
       		<property name="dataSource" ref="basicDataSource"></property>
       		<!-- 指定mybatis的局部xml文件的位置 -->
       		<property name="mapperLocations" value="cn/java/mapper/*.xml"></property>
       </bean>
       
       <!-- 将dao层中的方法与局部配置文件中的sql自动关联 -->
       <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       		<!-- 指定dao层类的位置 -->
       		<property name="basePackage" value="cn.java.mapper"></property>
       </bean>
       
</beans>

(3)User.java

package cn.java.entity;

public class User {
    private Long id;

    private String username;

    private String password;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
    }

}

(4)UserMapper.java

package cn.java.mapper;

import cn.java.entity.User;

public interface UserMapper {
    int deleteByPrimaryKey(Long id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

(5)USerMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.java.mapper.UserMapper" >
  <resultMap id="BaseResultMap" type="cn.java.entity.User" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, username, password
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from users
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from users
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="cn.java.entity.User" >
    insert into users (id, username, password
      )
    values (#{id,jdbcType=BIGINT}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="cn.java.entity.User" >
    insert into users
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="username != null" >
        username,
      </if>
      <if test="password != null" >
        password,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      </if>
      <if test="username != null" >
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="cn.java.entity.User" >
    update users
    <set >
      <if test="username != null" >
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.java.entity.User" >
    update users
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>

(6)UserService.java

/**
 * Project Name:dt55_spring_mybatis
 * File Name:UserService.java
 * Package Name:cn.java.service.impl
 * Date:下午5:33:54
 * Copyright (c) 2018, bluemobi All Rights Reserved.
 *
*/

package cn.java.service;

import cn.java.entity.User;


public interface UserService {

    User getUserById(Long id);

}

(7)UserServiceImpl.java

/**
 * Project Name:dt55_spring_mybatis
 * File Name:UserServiceImpl.java
 * Package Name:cn.java.service.impl
 * Date:下午5:32:42
 * Copyright (c) 2018, bluemobi All Rights Reserved.
 *
*/

package cn.java.service.impl;


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

import cn.java.entity.User;
import cn.java.mapper.UserMapper;
import cn.java.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper um;

    
    @Override
    public User getUserById(Long id) {
        return um.selectByPrimaryKey(id);
    }
}

(8)CaipinServlet.java

/**
 * Project Name:dt55_spring_mybatis
 * File Name:CaipinServlet.java
 * Package Name:cn.java.servlet
 * Date:下午5:34:56
 * Copyright (c) 2018, bluemobi All Rights Reserved.
 *
*/

package cn.java.servlet;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.java.entity.User;
import cn.java.service.impl.UserServiceImpl;

public class CaipinServlet {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserServiceImpl us = (UserServiceImpl) context.getBean("userService");
        User user = us.getUserById(2L);
        System.out.println(user);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值