1先创建一个动态web项目 添加tomcat服务器并测试tomcat是否正常启动
2进行目录规划
3配置spring 在lib下面加spring的架包 配置spring的配置文件 log4j2的配置文件 并创建测试类进行测试奇
4添加mybatis架包 mysql架包 配置mybatis配置文件 在spring配置文件中加载mybatis配置文件并进行测试
5创建数据库 创建pojo 创建mapper关联映射 并在spring的配置文件中加载关联映射 并在mapper里面开始写sql语句注意(namespase是dao的包名加类名 sql标签中的id要与dao接口中的方法名一致)
6创建dao
7创建service 创建service的实现类
8在spring配置文件中添加 datasource集数据库相关的一些配置 spring的自动扫描器集扫描注解 一个专门扫描dao接口的扫描器 开启事物管理
2目录规划
9测试增删改查![](https://img-blog.csdn.net/20161124221758840?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3spring配置文件
<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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
default-autowire="byName">
<!-- 开启spring的扫描类 就可以扫描control service等-->
<context:component-scan base-package="com.chenfeng"/>
<!-- 配置datasources的数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- jdbc相关 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 整合mybatis 加载mybatis的配置文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.cfg.common.xml"/>
<property name="mapperLocations" value="classpath:com/chenfeng/pojo/*.xml"></property>
</bean>
<!-- 专门配置扫描DAO接口扫描器 mappper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.chenfeng"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></bean>
<!-- 那些方法需要开事物 -->
<tx:advice id="taAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" no-rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" no-rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" no-rollback-for="Exception"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置那些类需要开启事物 -->
<aop:config>
<aop:pointcut expression="execution(* com.chenfeng.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="taAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
关联关系的配置文件增删改查以及pojo
package com.chenfeng.pojo;
import java.util.Date;
public class AUser
{
private int id;
private int roleId;
private String password;
private String name;
private Date createTime;
private Date updateTime;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public int getRoleId()
{
return roleId;
}
public void setRoleId(int roleId)
{
this.roleId = roleId;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Date getCreateTime()
{
return createTime;
}
public void setCreateTime(Date createTime)
{
this.createTime = createTime;
}
public Date getUpdateTime()
{
return updateTime;
}
public void setUpdateTime(Date updateTime)
{
this.updateTime = updateTime;
}
@Override
public String toString()
{
return "AUser [id=" + id + ", roleId=" + roleId + ", password=" + password + ", name=" + name + ", createTime="
+ createTime + ", updateTime=" + updateTime + "]";
}
}
<?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.chenfeng.dao.IUserDao">
<!-- 添加 -->
<insert id="insert" parameterType="com.chenfeng.pojo.AUser">
insert into a_user
(password,name,roleId, createTime,updateTime)
values
(#{password},#{name},#{roleId},#{createTime},#{updateTime})
</insert>
<!-- 查询一条 -->
<select id="findOne" parameterType="map" resultType="com.chenfeng.pojo.AUser">
select *from a_user where id = #{id}
</select>
<!-- 查询多条 -->
<select id="findCondList" resultType="com.chenfeng.pojo.AUser">
select *from a_user
</select>
<!-- 修改 -->
<update id="update" parameterType="com.chenfeng.pojo.AUser">
update a_user
<set>
<if test="password != null and password != '' ">
password = #{password},
</if>
<if test="roleId != null and roleId != '' ">
roleId = #{id}
</if>
</set>
<where>
id=#{id}
</where>
</update>
<!-- 删除 -->
<delete id="delete">
delete from a_user where id=#{id}
</delete>
</mapper>
dao层
package com.chenfeng.dao;
import java.util.List;
import java.util.Map;
import com.chenfeng.pojo.AUser;
public interface IUserDao
{
/**
* 增加操作
* @param user 为用户
* @return 返回值为主键
*/
int insert(AUser user);
/**
* 修改操作
* @param user 为用户
* @return 返回值为影响的条数
*/
int update(AUser user);
/**
* 删除操作
* @param user 为用户
* @return 返回值为影响的条数
*/
int delete(Map<String,Object> map);
/**
* 查找所有的操作
* @param map 为条件
* @return 用户的集合
*/
List<AUser> findCondList();
/**
* 查找满足条件的一条结果
* @param map 查询条件
* @return 为用户
*/
AUser findOne(Map<String,Object> map);
}
service层
//service接口类
package com.chenfeng.service;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.chenfeng.pojo.AUser;
public interface IUserservice
{
//增加
JSONObject insertUserService(AUser user);
//修改
JSONObject updateUserService(AUser user);
//删除
JSONObject deleteUserService(Map<String,Object> map);
//查找一条
AUser findOneUserService(Map<String,Object> map);
//查找多条
List<AUser> findListCondUserService();
}
//service实现类
package com.chenfeng.service.impl;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.chenfeng.dao.IUserDao;
import com.chenfeng.pojo.AUser;
import com.chenfeng.service.IUserservice;
@Service("service")
public class UserServiceImpl implements IUserservice
{
@Resource
IUserDao usedao;
public JSONObject insertUserService(AUser user)
{
JSONObject result = new JSONObject();
int a = this.usedao.insert(user);
if(a>0)
{
result.put("code", "0");
result.put("info", "插入成功");
}else
{
result.put("code", "1");
result.put("info", "插入失败");
}
return result;
}
@Override
public JSONObject updateUserService(AUser user)
{
JSONObject result = new JSONObject();
int a = this.usedao.update(user);
System.out.println("a的值是"+a);
if(a>0)
{
result.put("code", 0);
result.put("info", "更新成功");
JSONObject data = new JSONObject();
data.put("updta数据", user);
result.put("hello", data);
}else
{
result.put("code", 1);
result.put("info", "更新失败");
}
return result;
}
@Override
public JSONObject deleteUserService(Map<String, Object> map)
{
JSONObject result = new JSONObject();
int a = this.usedao.delete(map);
if(a<0)
{
result.put("code", 0);
result.put("info", "删除失败");
}else
{
result.put("code", 1);
result.put("info", "删除成功");
}
return result;
}
@Override
public AUser findOneUserService(Map<String, Object> map)
{
return this.usedao.findOne(map);
}
@Override
public List<AUser> findListCondUserService()
{
return this.usedao.findCondList();
}
}
测试类
package com.chenfeng.test;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import com.alibaba.fastjson.JSONObject;
import com.chenfeng.basetest.BaseTest;
import com.chenfeng.pojo.AUser;
import com.chenfeng.service.IUserservice;
import com.chenfeng.util.UtilConstantFinal;
public class TestMybatise extends BaseTest
{
private IUserservice userService;
@Before
public void initUser()
{
userService = (IUserservice) this.ac.getBean("service");
}
//查找一条记录
@Test
public void findOne()
{
Map<String,Object> map = new HashMap<String,Object>();
map.put("id", "2");
AUser user = this.userService.findOneUserService(map);
UtilConstantFinal.LOGGER.info("user{}",user);
}
@Test
//查找所有的用户
public void findList()
{
List<AUser> user = this.userService.findListCondUserService();
UtilConstantFinal.LOGGER.info("List集合的值为{}",user);
}
//插入一条记录
@Test
public void insertUser()
{
AUser user = new AUser();
user.setCreateTime(new Date());
user.setName("陈峰");
user.setPassword("35533");
user.setRoleId(888);
user.setUpdateTime(new Date());
JSONObject result = this.userService.insertUserService(user);
UtilConstantFinal.LOGGER.info("result{}",result);
}
//更新用户
@Test
public void updateUser()
{
AUser user = new AUser();
user.setCreateTime(new Date());
user.setId(4);
user.setPassword("555llllllss");
JSONObject json = this.userService.updateUserService(user);
UtilConstantFinal.LOGGER.info("json{}",json);
}
//删除用户操作
@Test
public void deleteUser()
{
Map<String,Object> map = new HashMap<String,Object>();
map.put("id", "3");
JSONObject result = this.userService.deleteUserService(map);
UtilConstantFinal.LOGGER.info("删除信息提示result{}",result);
}
}