1、首先建立项目java web
2、导入mybatis所需的jar包
mybatis需要jar包:mybatis-3.3.0.jar
mysql驱动jar包:mysql-connector-java-5.1.15.-bin.jar
日志记录jar包:log4j.jar
3、创建数据库数据(以mysql为例子)
4、编写配置文件
①database.properties代码:
user=root
password=123456
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/webshop?useUnicode=true&characterEncoding=utf8
initialPoolSize=5
maxPoolSize=150
②log4j.properties代码:
# Root logger option
log4j.rootLogger=INFO, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.systemOut.Threshold = DEBUG
log4j.appender.systemOut.ImmediateFlush = TRUE
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file1=org.apache.log4j.RollingFileAppender
log4j.appender.file1.File=E\:/tomcat/logs/NPES-Rolling.log
log4j.appender.file1.MaxFileSize=5MB
log4j.appender.file1.MaxBackupIndex=10
log4j.appender.file1.layout=org.apache.log4j.PatternLayout
log4j.appender.file1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File=E\:/tomcat/logs/NPES-Daily.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=INFO
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %-5p %c{1}\:%L - %m%n
#
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
③spring-mvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.shop.controller" />
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
<mvc:interceptors>
<bean class="com.shop.interceptor.basePathInerrapertor">bean>
mvc:interceptors>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8value>
property>
<property name="maxUploadSize">
<value>524288000value>
property>
<property name="maxInMemorySize">
<value>4096value>
property>
bean>
beans>
④spring-mybatis.xml
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<context:component-scan base-package="com.shop.service.impl">context:component-scan>
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}">property>
<property name="password" value="${password}">property>
<property name="driverClass" value="${driverClass}">property>
<property name="jdbcUrl" value="${jdbcUrl}">property>
<property name="initialPoolSize" value="${initialPoolSize}">property>
<property name="maxPoolSize" value="${maxPoolSize}">property>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/shop/mapping/*.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.shop.mapper">property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
beans>
5、创建方法接口UserMapper.java和定义操作user表的sql映射文件UserMapping.xml
package com.shop.mapper;
import java.util.List;
import java.util.Map;
import com.shop.entity.User;
public interface UserMapper {
//登录
public User login(User user);
//查询用户订单信息
public List<User> findOrdersListResultMap(int user_id);
//查询订单明细
public List<User> findOrdersDetailList(int user_id);
//根据用户名查询用户信息
public User findByName(String username);
//查询用户订单商品明细
public List<User> findOrdersItemList(int user_id);
}
<mapper namespace="com.shop.mapper.UserMapper">
<resultMap type="com.shop.entity.User" id="userMap">
<result property="user_id" column="user_id"/>
<result property="username" column="username"/>
<result property="pwd" column="pwd"/>
<result property="address" column="address"/>
resultMap>
<sql id="us">
user_id,username,pwd,address
sql>
<select id="login" parameterType="com.shop.entity.User" resultMap="userMap">
select <include refid="us">include> from user
<where>
username=#{username} and pwd=#{pwd}
where>
select>
<select id="findByName" resultMap="userMap">
select * from user where username=#{username}
select>
<select id="findOrdersListResultMap" resultType="com.shop.entity.OrdersCustom">
SELECT
orders.orders_id,
orders.user_id,
orders.createtime,
user.username,
user.address
FROM
orders,user
<where>
orders.user_id = user.user_id and user.user_id=#{user_id}
where>
select>
<select id="findOrdersDetailList" resultType="com.shop.entity.Userorderdetail">
SELECT
orders.orders_id,
orders.createtime,
user.username,
user.address,
orderdetail.items_id,
orderdetail.items_num
FROM orders,user,orderdetail
<where>
orders.user_id = user.user_id
and orders.orders_id =orderdetail.orders_id and user.user_id=#{user_id}
where>
select>
<select id="findOrdersItemList" statementType="CALLABLE" resultType="com.shop.entity.Userorderitem">
{call QUERYITEMSBYUSER_ID(
#{user_id, jdbcType=INTEGER, mode=IN}
)}
select>
mapper>
6、创建方法接口UserService.java和接口实现类UserServiceImpl.java
package com.shop.service;
import java.util.List;
import com.shop.entity.User;
import com.shop.entity.Userorderitem;
public interface UserService {
//登录
public User getlogin(User user);
//查询用户订单信息
public List findOrdersListResultMap(String username);
//查询订单明细
public List findOrdersDetailList(String username);
//查询用户订单商品明细
public List findOrdersItemList(String username);
}
package com.shop.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shop.mapper.UserMapper;
import com.shop.entity.User;
import com.shop.entity.Userorderitem;
import com.shop.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper;
//用户登录
public User getlogin(User user) {
System.out.println("****1");
User userinfo = userMapper.login(user);
System.out.println("****2");
return userinfo;
}
//查询用户订单信息
public List findOrdersListResultMap(String username) {
User user = userMapper.findByName(username);
int user_id = user.getUser_id();
System.out.println("****3");
List userlist = userMapper.findOrdersListResultMap(user_id);
System.out.println("****4");
return userlist;
}
public List findOrdersDetailList(String username) {
User user = userMapper.findByName(username);
int user_id = user.getUser_id();
System.out.println("****5 id="+user_id);
List userlist = userMapper.findOrdersDetailList(user_id);
System.out.println("****6");
return userlist;
}
public List findOrdersItemList(String username) {
User user = userMapper.findByName(username);
int user_id = user.getUser_id();
System.out.println("****7 id="+user_id);
List userlist = userMapper.findOrdersItemList( user_id);
System.out.println("****8");
return userlist;
}
}
7、编写控制层代码与前台(略)由于时间关系,会在后续博文进行补充。
8、测试(略)