Spring+Mybatis+Maven+MySql搭建实例
Posted 緈諨の約錠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring+Mybatis+Maven+MySql搭建实例相关的知识,希望对你有一定的参考价值。
一、准备工作
1. 首先创建一个表:
- CREATE TABLE `t_user` (
- `USER_ID` int(11) NOT NULL AUTO_INCREMENT,
- `USER_NAME` char(30) NOT NULL,
- `USER_PASSWORD` char(10) NOT NULL,
- `USER_EMAIL` char(30) NOT NULL,
- PRIMARY KEY (`USER_ID`),
- KEY `IDX_NAME` (`USER_NAME`)
- ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
随便插入一些数据:
- INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (1, \'林炳文\', \'1234567@\', \'ling20081005@126.com\');
- INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (2, \'evan\', \'123\', \'fff@126.com\');
- INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (3, \'kaka\', \'cadg\', \'fwsfg@126.com\');
- INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (4, \'simle\', \'cscs\', \'fsaf@126.com\');
- INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (5, \'arthur\', \'csas\', \'fsaff@126.com\');
- INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (6, \'小德\', \'yuh78\', \'fdfas@126.com\');
- INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (7, \'小小\', \'cvff\', \'fsaf@126.com\');
- INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (8, \'林林之家\', \'gvv\', \'lin@126.com\');
- INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (9, \'林炳文Evankaka\', \'dfsc\', \'ling2008@126.com\');
- INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (10, \'apple\', \'uih6\', \'ff@qq.com\');
二、工程创建
1、Maven工程创建
(1)新建
(2)选择快速框架
(3)输出项目名,包,记得选war(表示web项目,以后可以spingMVC连起来用)
(4)创建好之后
目录如下:
(5)检查下
这三个地方JDK的版本一定要一样!!!!
三、sping+mybatis配置
1、整个工程目录如下:
2、POM文件
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.lin</groupId>
- <artifactId>ssm_project</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <properties>
- <!-- spring版本号 -->
- <spring.version>3.2.8.RELEASE</spring.version>
- <!-- log4j日志文件管理包版本 -->
- <slf4j.version>1.6.6</slf4j.version>
- <log4j.version>1.2.12</log4j.version>
- <!-- junit版本号 -->
- <junit.version>4.10</junit.version>
- <!-- mybatis版本号 -->
- <mybatis.version>3.2.1</mybatis.version>
- </properties>
- <dependencies>
- <!-- 添加Spring依赖 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!--单元测试依赖 -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>${junit.version}</version>
- <scope>test</scope>
- </dependency>
- <!-- 日志文件管理包 -->
- <!-- log start -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>${log4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <!-- log end -->
- <!--spring单元测试依赖 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring.version}</version>
- <scope>test</scope>
- </dependency>
- <!--mybatis依赖 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>${mybatis.version}</version>
- </dependency>
- <!-- mybatis/spring包 -->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.2.0</version>
- </dependency>
- <!-- mysql驱动包 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.29</version>
- </dependency>
- </dependencies>
- </project>
3、java代码-------src/main/java
目录如下:
(1)User.java
对应数据库中表的字段,放在src/main/java下的包com.lin.domain
- package com.lin.domain;
- /**
- * User映射类
- *
- * @author linbingwen
- * @time 2015.5.15
- */
- public class User {
- private Integer userId;
- private String userName;
- private String userPassword;
- private String userEmail;
- public Integer getUserId() {
- return userId;
- }
- public void setUserId(Integer userId) {
- this.userId = userId;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getUserPassword() {
- return userPassword;
- }
- public void setUserPassword(String userPassword) {
- this.userPassword = userPassword;
- }
- public String getUserEmail() {
- return userEmail;
- }
- public void setUserEmail(String userEmail) {
- this.userEmail = userEmail;
- }
- @Override
- public String toString() {
- return "User [userId=" + userId + ", userName=" + userName
- + ", userPassword=" + userPassword + ", userEmail=" + userEmail
- + "]";
- }
- }
(2)UserDao.java
Dao接口类,用来对应mapper文件。放在src/main/java下的包com.lin.dao,内容如下:
- package com.lin.dao;
- import com.lin.domain.User;
- /**
- * 功能概要:User的DAO类
- *
- * @author linbingwen
- * @since 2015年9月28日
- */
- public interface UserDao {
- /**
- *
- * @author linbingwen
- * @since 2015年9月28日
- * @param userId
- * @return
- */
- public User selectUserById(Integer userId);
- }
(2)UserService.java和UserServiceImpl.java
service接口类和实现类,放在src/main/java下的包com.lin.service,内容如下:
UserService.java
- package com.lin.service;
- import org.springframework.stereotype.Service;
- import com.lin.domain.User;
- /**
- * 功能概要:UserService接口类
- *
- * @author linbingwen
- * @since 2015年9月28日
- */
- public interface UserService {
- User selectUserById(Integer userId);
- }
UserServiceImpl.java
- package com.lin.service;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.lin.dao.UserDao;
- import com.lin.domain.User;
- /**
- * 功能概要:UserService实现类
- *
- * @author linbingwen
- * @since 2015年9月28日
- */
- @Service
- public class UserServiceImpl implements UserService{
- @Autowired
- private UserDao userDao;
- public User selectUserById(Integer userId) {
- return userDao.selectUserById(userId);
- }
- }
(4)mapper文件
用来和dao文件对应,放在src/main/java下的com.lin.mapper包下
- <?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="com.lin.dao.UserDao">
- <!--设置domain类和数据库中表的字段一一对应,注意数据库字段和domain类中的字段名称不致,此处一定要!-->
- <resultMap id="BaseResultMap" type="com.lin.domain.User">
- <id column="USER_ID" property="userId" jdbcType="INTEGER" />
- <result column="USER_NAME" property="userName" jdbcType="CHAR" />
- <result column="USER_PASSWORD" property="userPassword" jdbcType="CHAR" />
- <result column="USER_EMAIL" property="userEmail" jdbcType="CHAR" />
- </resultMap>
- <!-- 查询单条记录 -->
- <select id="selectUserById" parameterType="int" resultMap="BaseResultMap">
- SELECT * FROM t_user WHERE USER_ID = #{userId}
- </select>
- </mapper>
4、资源配置-------src/main/resources
目录如下:
(1)mybatis配置文件
这里没有什么内容,因为都被放到application.xml中去了,放在src/main/resources下的mybatis文件夹下
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>
- </configuration>
(2)数据源配置jdbc.properties
放在src/main/resources下的propertiesy文件夹下
- jdbc_driverClassName=com.mysql.jdbc.Driver
- jdbc_url=jdbc:mysql://localhost:3306/learning
- jdbc_username=root
- jdbc_password=christmas258@
(3)Spring配置
这是最重要的:application.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:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <!-- 引入jdbc配置文件 -->
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:properties/*.properties</value>
- <!--要是有多个配置文件,只需在这里继续添加即可 -->
- </list>
- </property>
- </bean>
- <!-- 配置数据源 -->
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <!-- 不使用properties来配置 -->
- <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/learning" />
- <property name="username" value="root" />
- <property name="password" value="christmas258@" /> -->
- <!-- 使用properties来配置 -->
- <property name="driverClassName">
- <value>${jdbc_driverClassName}</value>
- </property>
- <property name="url">
- <value>${jdbc_url}</value>
- </property>
以上是关于Spring+Mybatis+Maven+MySql搭建实例的主要内容,如果未能解决你的问题,请参考以下文章