标签:def public web log4 har autowired xxxx space set
一、准备工作
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文件
3、java代码-------src/main/java
目录如下:
(1)User.java
对应数据库中表的字段,放在src/main/java下的包com.lin.domain
- package com.lin.domain;
-
- 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;
-
- public interface UserDao {
-
- 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;
-
- 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;
-
- @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">
- <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">
-
-
- <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">
-
- <!-- <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@" /> -->
-
- <property name="driverClassName">
- <value>${jdbc_driverClassName}</value>
- </property>
- <property name="url">
- <value>${jdbc_url}</value>
- </property>
- <property name="username">
- <value>${jdbc_username}</value>
- </property>
- <property name="password">
- <value>${jdbc_password}</value>
- </property>
- </bean>
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage"
- value="com.lin.dao" />
- </bean>
-
-
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mapperLocations" value="classpath*:com/lin/mapper/**/*.xml"/>
- <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
- <!-- <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model"
- /> -->
- </bean>
-
-
- <context:component-scan base-package="com.lin.service" />
-
- </beans>
(4)日志打印log4j.properties
就放在src/main/resources
- log4j.rootLogger=DEBUG,Console,Stdout
-
- #Console
- log4j.appender.Console=org.apache.log4j.ConsoleAppender
- log4j.appender.Console.layout=org.apache.log4j.PatternLayout
- log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
-
- log4j.logger.java.sql.ResultSet=INFO
- log4j.logger.org.apache=INFO
- log4j.logger.java.sql.Connection=DEBUG
- log4j.logger.java.sql.Statement=DEBUG
- log4j.logger.java.sql.PreparedStatement=DEBUG
-
- log4j.appender.Stdout = org.apache.log4j.DailyRollingFileAppender
- log4j.appender.Stdout.File = E://logs/log.log
- log4j.appender.Stdout.Append = true
- log4j.appender.Stdout.Threshold = DEBUG
- log4j.appender.Stdout.layout = org.apache.log4j.PatternLayout
- log4j.appender.Stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
四、单元测试
上面的配置完好,接下来就是测验成功
整个目录 如下:
(1)测试基类
- package com.lin.baseTest;
-
- import org.junit.runner.RunWith;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
- @ContextConfiguration(locations = { "classpath:application.xml" })
- @RunWith(SpringJUnit4ClassRunner.class)
- public abstract class SpringTestCase extends AbstractJUnit4SpringContextTests{
- protected Logger logger = LoggerFactory.getLogger(getClass());
- }
(2)测试类
- package com.lin.service;
-
- import org.apache.log4j.Logger;
- import org.junit.Test;
- import org.springframework.beans.factory.annotation.Autowired;
-
- import com.lin.baseTest.SpringTestCase;
- import com.lin.domain.User;
-
- public class UserServiceTest extends SpringTestCase {
- @Autowired
- private UserService userService;
- Logger logger = Logger.getLogger(UserServiceTest.class);
-
- @Test
- public void selectUserByIdTest(){
- User user = userService.selectUserById(10);
- logger.debug("查找结果" + user);
- }
-
-
- }
选中selectUserByIdTest,然后右键如下运行
输出结果:
重要打印的结果
这里
- 2015-09-28 15:20:15,129 [main] DEBUG [com.lin.dao.UserDao.selectUserById] - ==> Preparing: SELECT * FROM t_user WHERE USER_ID = ?
- 2015-09-28 15:20:15,160 [main] DEBUG [com.lin.dao.UserDao.selectUserById] - ==> Parameters: 10(Integer)
- 2015-09-28 15:20:15,160 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6b64bff9]
- 2015-09-28 15:20:15,160 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
- 2015-09-28 15:20:15,160 [main] DEBUG [com.lin.service.UserServiceTest] - 查找结果User [userId=10, userName=apple, userPassword=uih6, userEmail=ff@qq.com]
数据库:
程序成功运行,并且结果正确!
Spring+Mybatis+Maven+MySql搭建实例
标签:def public web log4 har autowired xxxx space set
原文地址:https://www.cnblogs.com/summary-2017/p/9053674.html