需要引入的jar包
idea引入jar
- 因为没有用maven管理所以只能手动导入jar包
第一步: 将jar包复制到项目中的lib文件下
// mybais用到的
log4j.jar
mybatis-3.2.5.jar
ojdbc6.jar
commons-dbcp-1.2.2.jar
mybatis-spring-1.2.2.jar
commons-pool.jar
// aop所需的包
aspectjweaver.jar
// json解析用到的
jackson-annotations-2.8.0.jar
jackson-core-2.8.7.jar
jackson-databind-2.8.7.jar
编辑文件 项目-->.idea-->libraries-->Spring-4.3.0.RELEASE.xml
在<CLASSES> 标签中添加jar包的引用
<root url="jar://$PROJECT_DIR$/lib/log4j.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/mybatis-3.2.5.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/ojdbc6.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/commons-dbcp-1.2.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/mybatis-spring-1.2.2.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/commons-pool.jar!/" />
- tips: 添加完成之后保存下
创建springMVC项目
新建项目
-
File --> new --> Project 选好后点击Next
新建项目
取个项目名称点击Finish

整理目录结构


配置tomcat服务



点击Fix ---> 点击 apply --->ok


创建User
创建位置 com.yidespring.entity
package com.yidespring.entity;
/**
* Created by yide on 2018/11/20.
*/
public class User {
private Integer id;
private String nickname;
private String password;
private String firstName;
private String lastName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", nickname='" + nickname + '\'' +
", password='" + password + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
创建UserMapper
创建位置 com.yidespring.dao
package com.yidespring.dao;
import com.yidespring.entity.User;
import java.util.List;
/**
* Created by yide on 2018/11/20.
*/
public interface UserMapper {
int insert(User item);
User selectByPrimaryKey(int key);
List<User> selectField(User item);
int updateByPrimaryKey(User item);
}
创建UserService
创建位置 com.yidespring.service
package com.yidespring.service;
import com.yidespring.dao.UserMapper;
import com.yidespring.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by yide on 2018/11/20.
*/
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public User createUser(User item){
userMapper.insert(item);
return item;
}
}
创建UserController
创建位置 com.yidespring.controller
package com.yidespring.controller;
import com.yidespring.entity.User;
import com.yidespring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
/**
* Created by yide on 2018/11/20.
*/
// 配置事务
//@Transactional(rollbackFor = ClassNotFoundException.class)
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping(value = "test",method = RequestMethod.GET)
@ResponseBody
public Object test(
@RequestParam(value = "name",required = true) String name
){
List<String> list = new ArrayList<>();
list.add("name");
list.add("名称");
list.add(name);
return list;
}
@RequestMapping(value = "test_create",method = RequestMethod.GET)
@ResponseBody
public Object testCreate(
@RequestParam(value = "nickname",required = true) String nickname,
@RequestParam(value = "id",required = true) Integer id,
@RequestParam(value = "password",required = true) String password
){
User item = new User();
item.setId(id);
item.setNickname(nickname);
item.setPassword(password);
userService.createUser(item);
return item;
}
// 事务测试 要么全部执行全部sql要么不执行
@RequestMapping(value = "test_affair",method = RequestMethod.GET)
@ResponseBody
public Object testAffair(
@RequestParam(value = "nickname",required = true) String nickname,
@RequestParam(value = "id",required = true) Integer id,
@RequestParam(value = "password",required = true) String password
){
User item = new User();
item.setId(id);
item.setNickname(nickname);
item.setPassword(password);
userService.createUser(item);
Integer.valueOf("asd");
item.setId(++id);
userService.createUser(item);
return item;
}
}
创建UserMapper.xml
创建位置resources下 mybatis.com.yidespring.mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yideaffair.dao.UserMapper">
<insert id="insert" parameterType="com.yidespring.entity.User">
insert into user(id,nickname,password,first_name,last_name)
values(#{id,jdbcType=INTEGER},#{nickname,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR},
#{firstName,jdbcType=VARCHAR},#{lastName,jdbcType=VARCHAR})
</insert>
</mapper>
创建dev.jdbc.properties数据库文件
文件位置 项目-->src-->main-->resources-->web-->dev.jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/springdemo?characterEncoding=utf-8
username=root
password=root
#链接5个
maxActive=5
#等待5秒
maxWait=5000
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
配置dispatcher-servlet.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:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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">
<!-- 加载数据库配置文件 -->
<util:properties id="jdbc" location="dev.jdbc.properties"></util:properties>
<!-- 配置数据源 -->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="#{jdbc.url}"></property>
<property name="driverClassName" value="#{jdbc.driver}"></property>
<property name="username" value="#{jdbc.username}"></property>
<property name="password" value="#{jdbc.password}"></property>
<property name="maxActive" value="#{jdbc.maxActive}"></property>
<property name="maxWait" value="#{jdbc.maxWait}"></property>
</bean>
<!-- 注入jdbcTemplate服务 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<!-- 配置mybatis的session工厂 给mapper.xml注入数据库链接 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="mapperLocations" value="classpath*:mybatis.com.yidespring.mapper/*.xml" ></property>
</bean>
<!-- 扫描指定包下面的所有接口,来 匹配和映射我们的sql数据 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yidespring.dao"></property>
</bean>
<!-- 声明事务管理组件 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"></property>
</bean>
<!-- 方式一: 开启事务注解扫描 -->
<!--<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"></tx:annotation-driven>-->
<!-- 方式二: apo切面方式配置事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 切入create开头的所有方法 -->
<tx:method name="create*" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<!-- 用切面的方式配置进去 -->
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice" pointcut="within(com.yidespring.service..*)"></aop:advisor>
</aop:config>
<!-- 注意这里的顺序,扫包不能放在关联mybatis之前 -->
<!--开启spring扫包 -->
<context:component-scan base-package="com.yidespring"></context:component-scan>
<!-- 开启MVC注解扫描组件 -->
<mvc:annotation-driven/>
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
数据库结构

运行项目
访问接口


网友评论