不需要maven的SSM框架整合

第一步必不可少,添加相关的jar包

 

 

下载地址:SSM-jar地址

(下载需要拷贝地址到浏览器窗口,写了一上午挺累的,愿意的客官赏两个积分,网上也有很多也可以自己照着图上的下载对应的版本)

 

 

后面所提到的整个的文件目录(部分springmvc)

第二步:当然就连接数据库,所以我们先配置mybatis以及mybatis和spring的整合

 

1)创建一个源文件sources,然后再改文件下创建一个名为mybatis-config.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>
<!--   	配置全局属性 -->
  		<settings>
<!--   		使用jdbc的getGeneratedKeys获取数据库自增主键 -->
  		<setting name="useGeneratedKeys" value="true"/>
  		
<!--   		使用列别名替换列名  默认true
		select name as title from table
-->
		<setting name="useColumnLabel" value="true"/>
		
<!-- 		开启驼峰命名转化  table(create_time) -> entity(createTime)-->
		<setting name="mapUnderscoreToCamelCase" value="true"/>
  		</settings>

  
  </configuration>

 

2)接下来需要配置spring-dao层的整合,但是在这之前我们需要准备数据库,以及我们的连接参数的属性文件。

 

2.1这里只是为了演示我就创建一个简单的表,只包含用户名,密码,年龄(使用MySQL数据库中springmvc数据库以及user表)

mysql> create database springmvc;
Query OK, 1 row affected (0.00 sec)

mysql> use springmvc
Database changed
mysql> CREATE TABLE user(
    -> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    -> user_name VARCHAR(20) NOT NULL,
    -> password VARCHAR(15) NOT NULL,
    -> age TINYINT);

2.2接下来我们就需要一个属性文件用来保存我们的数据库连接驱动名以及用户名等参数,在resources目录下创建一个resource.properties文件(以键值对保存参数的文件),内容如下:

jdbc.name=你的数据库用户名
jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.pwd=你的数据库密码
jdbc.url=jdbc\:mysql\://localhost\:3306/springmvc?useUnicode\=true&characterEncoding\=utf8

2.3接下来我们就在resources/spring/下创建一个spring-dao.xml来整合mybatis和spring

<?xml version="1.0" encoding="UTF-8"?>  
<beans 
	xmlns="http://www.springframework.org/schema/beans" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xsi:schemaLocation="  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
    http://www.springframework.org/schema/cache  
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/util  
    http://www.springframework.org/schema/util/spring-util.xsd">  
<!--  	配置整合mybatis过程 -->
		
<!-- 		1配置数据库参数,引入刚才写的属性文件 -->
			<context:property-placeholder location="classpath:resource.properties"/>
<!-- 		2	数据库连接池 (c3p0)-->
			<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
			<property name="driverClass" value="${jdbc.driverclass}"></property>
	  		<property name="jdbcUrl" value="${jdbc.url}"></property>
	  		<property name="user" value="${jdbc.name}"></property>
	  		<property name="password" value="${jdbc.pwd}"></property>
	  		
<!-- 	  		c3p0私有属性 -->

			<property name="maxPoolSize" value="30"></property>
			<property name="minPoolSize" value="10"></property>
<!-- 			关闭后不自动提交 -->
			<property name="autoCommitOnClose" value="false"></property>
<!-- 			连接超时时间 -->
			<property name="checkoutTimeout" value="1000"></property>
<!-- 			连接失败重试次数 -->
			<property name="acquireRetryAttempts" value="2"></property>
			
			</bean>
			
<!-- 		3需要使用mybatis则这里需要配置sqlsessionFactory对象 -->
		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 				注入数据库连接池 
					在单独的mybatis中是在configuration中配置的数据库连接,在这里mybatis值配置了mapper
					才能获取sqlSessionFactory对象
					所以需要为这个类注入数据库连接池
-->
					<property name="dataSource" ref="dataSource"></property>
<!-- 					mybatis全局配置文件 -->
					<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 						扫描entity包使用别名,不然前面配置的mapper直接使用的类名会出错 
 		多个包需要扫描	<property name="typeAliasesPackage" value="org.seckill.entity;org.seckill.entity2"/> -->
					<property name="typeAliasesPackage" value="org.mtest.entity"></property>
					
<!-- 					扫描sql配置文件也就是mapper里面的 -->

					<property name="mapperLocations" value="classpath:mapper/*.xml"></property>

			</bean>
			
<!-- 		4	配置扫描dao接口包,动态实现dao接口,自动注入到spring容器中 -->
			<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 				注入到sqlSessionFactory -->
				<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 				给出需要扫描的dao包 -->
				<property name="basePackage" value="org.mtest.dao"></property>
			</bean>
 </beans>

3)上面配置已经差不多了,接下来在src下创建两个包org.mtest.entity,org.mtest.dao

3.1在entity下面创建一个实体javaBean,User

package org.mtest.entity;


public class User {
		private int id;
		private String userName;
		private String password;
		private int age;
		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;
		}
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		
}

 

 

3.2位该实体类创建一个接口dao用来对其进行数据操作

 

package org.mtest.dao;

import org.apache.ibatis.annotations.Param;
import org.mtest.entity.User;

public interface UserDao {
	/**
	 * 通过id查询用户
	 * @param id
	 * @return
	 */
		 User queryById(@Param("id")int id);
}

4)接下来就是需要配置mybatis的mapper了,首先在resources下面创建一个和spring同级的mapper文件,并创建一个userDao.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="org.mtest.dao.UserDao">
        	<select id="queryById" resultType="User">
        		select user_name,password,age
        		from user
        		where id = #{id}
        	</select>
        </mapper>

 

 

 

 

 

 

经过上面的配置spring和mybatis已经整合完毕了,接下来我们测试一下(由于spring的版本我没有统一好导致了junit不能进行单元测试),这里我直接使用main函数进行测试(上面的截图少了三个包,在下载的文件里面包含了所有)

 

测试之前首先插入一条数据  

insert into user(user_name,password,age)

values('zhangSan','123456','20');

 

编写一个测试类

 

 

package test.user;

import org.mtest.dao.UserDao;
import org.mtest.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestUserDao {
	private static UserDao userDao;
	
	
	
	public static void main(String[] args) {
		//加载容器
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-dao.xml");
		//bean的名字与接口名相同但首字母小写
		userDao = (UserDao) context.getBean("userDao");
		User user = userDao.queryById(1);
		System.out.println(user.getUserName() +"****"+user.getPassword()+"***"+user.getAge());
	}
	
}
 

测试结果日志(需要在resources文件下创建一个xml文件再会有日志输出,该日志)

 

 

 

 

日志lofback.xml文件内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

 

 

 

运行结果为:可以从日志中看到我们调用dao中的方法时执行了查询语句,并成功返回了我之前插入的数据

11:17:00.563 [main] DEBUG org.mtest.dao.UserDao.queryById - ==>  Preparing: select user_name,password,age from user where id = ? 
11:17:00.660 [main] DEBUG org.mtest.dao.UserDao.queryById - ==> Parameters: 1(Integer)
11:17:00.699 [main] DEBUG org.mtest.dao.UserDao.queryById - <==      Total: 1
11:17:00.706 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7e7be63f]
zhangSan****123456***20

 

都此处spring和mybatis的整合就完成了,接下来是springmvc了,我在下节当中进行整理

 

评论 31
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值