1. mybatis的映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.lky.dao.UserMapper">
<insert id="add" parameterType="com.lky.model.User">
insert into s_user(user_name,user_birthday,user_salary) values(#{name},#{birthday},#{salary})
</insert> <update id="update" parameterType="com.lky.model.User">
update s_user set user_name=#{name},user_birthday=#{birthday},user_salary=#{salary} where user_id=#{id}
</update> <delete id="delete" parameterType="int">
delete from s_user where user_id=#{id}
</delete> <select id="findById" parameterType="int" resultType="com.lky.model.User">
select user_id id,user_name name,user_birthday birthday,user_salary salary from s_user where user_id=#{id}
</select> <select id="findAll" resultType="com.lky.model.User">
select user_id id,user_name name,user_birthday birthday,user_salary salary from s_user
</select> </mapper>

2.mybatis的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration SYSTEM "http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<mappers>
<mapper resource="com/lky/dao/UserMapper.xml"/>
</mappers>
</configuration>

3.Spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 配置数据源 DriverManagerDataSource-->
<bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://10.21.25.228:3306/mybatis</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean> <!-- mybatis的sqlSession的工厂 sqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<property name="typeAliasesPackage" value="com.lky.model"/>
</bean> <!-- mybatis自动扫描加载sql映射文件、接口:MapperScannerConfigurer
basePackage:指定sql映射文件,接口所在的包(自动扫描)
sqlSessionFactory: 引用上面定义的sqlSessionFactory
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lky.dao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean> <!-- 事务管理 DataSourceTransactionManager
dataSource:引用上面定义的数据源
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="jdbcDataSource"/>
</bean> <!-- 使用声明的事务
使用上面定义的事务
-->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>

4.实体类

package com.lky.model;

import java.util.Date;

public class User {
private int id;
private String name;
private Date birthday;
private double salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public User(int id, String name, Date birthday, double salary) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
this.salary = salary;
}
public User() {
super();
} @Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", birthday=" + birthday
+ ", salary=" + salary + "]";
} }

5.接口类

package com.lky.dao;

import java.util.List;
import com.lky.model.User; public interface UserMapper {
public void add(User user);
public void update(User user);
public void delete(User user);
public void findById(int id);
public List<User> findAll(); }

6.测试文件

package com.lky.test;

import java.util.Date;
import java.util.List; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.lky.dao.UserMapper;
import com.lky.model.User; @RunWith(SpringJUnit4ClassRunner.class)//使用Spring的测试框架
@ContextConfiguration("/beans.xml")//加载spring的配置文件bean
public class smTest { @Autowired //自动注入该对象
private UserMapper userMapper; @Test
public void testAdd(){
User user=new User(-1,"tom",new Date(),12345);
userMapper.add(user); } @Test
public void testfindByName(){
System.out.println(userMapper.findByName("tom")); } @Test
public void testfingAll(){
List<User> ss=userMapper.findAll();
System.out.println(ss);
}
}

Spring+Mybatis+mysql配置的更多相关文章

  1. SpringMVC+Spring+mybatis项目从零开始--Spring mybatis mysql配置实现

    上一章我们把SSM项目结构已搭建(SSM框架web项目从零开始--分布式项目结构搭建)完毕,本章将实现Spring,mybatis,mysql等相关配置. 1.    外部架包依赖引入 外部依赖包引入 ...

  2. SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置

    转载:http://blog.csdn.net/xiadi934/article/details/50786293 项目环境: 在SpringMVC +Spring + MyBatis + MySQL ...

  3. freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建

    今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven ...

  4. springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

    @_@ 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) myba ...

  5. Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建

    目录 Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建 0.项目准备 1.数据持久层Mybatis+MySQL 1.1 MySQL数据准备 1.2 Mybatis ...

  6. maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

    转自:https://www.cnblogs.com/lmei/p/7190755.html?utm_source=itdadao&utm_medium=referral @_@ 写在最前 之 ...

  7. SpringMVC+Spring+Mybatis+Mysql项目搭建

    眼下俺在搭建一个自己的个人站点玩玩.一边练习.一边把用到的技术总结一下,日后好复习. 站点框架大致例如以下图所看到的: 眼下仅仅用到了SpringMVC+Spring+Mybatis+Mysql.把它 ...

  8. 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解

    http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...

  9. SpringMVC+Mybatis+MySQL配置Redis缓存

    SpringMVC+Mybatis+MySQL配置Redis缓存 1.准备环境: SpringMVC:spring-framework-4.3.5.RELEASE-dist Mybatis:3.4.2 ...

随机推荐

  1. [转] linux 信号量之SIGNAL

    我们可以使用kill -l查看所有的信号量解释,但是没有看到SIGNAL 0的解释. [root@testdb~]# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) ...

  2. 拓扑排序 HDU1285

    这个题是个模板题,可以直接用拓扑排序的模板来做, AC代码 #include <stdio.h> #include<iostream> #include <string. ...

  3. 常用html元素的取值和赋值方法总结

    1.获得type类型为TEXT或者AREATEXT的值 var textval = $("#text_id").attr("value"); var textv ...

  4. 安装Linux和Windows的双系统

    平时使用较多的操作系统是Windows,想玩玩Linux平时也是在虚拟机上,强迫症的怎么能忍,一直想装个双系统,也能强迫自己练习Linux命令,之前重装系统的时候也试着装了一下,但是准备不够充分.结果 ...

  5. Android布局文件-错误

    View requires API level 14 (current min is 8): <?xml version="1.0" encoding="utf-8 ...

  6. JavaScript中几个可以转化为false的值

    1.[0,NaN,“”,null,undefined]都可以直接转化为false,但这几个值不是完全相等的 var arr = [0,"",false,null,undefined ...

  7. js动态新增组合Input标签

    var x = 1; function addlink() { var linkdiv = document.getElementById("add1_0"); if (linkd ...

  8. 破解Windows Server 2003只允许3个用户远程登陆

    导读:WIN2003在使用远程桌面登录的时候,一台机器默认情况下只允许3个用户同时登录. 这很不方便.我们修改WIN2003远程桌面的连接数,可以设置3个以上用户远程桌面. 1.启动终端服务:在&qu ...

  9. Java实现多线程下载

    package cn.test.DownLoad; import java.io.File; import java.io.InputStream; import java.io.RandomAcce ...

  10. Oracle数据库之PL/SQL程序设计基础

    PL/SQL程序设计基础 一.PL/SQL块结构 前边我们已经介绍了PL/SQL块的结构,再来回顾一下: DECLARE /* * 声明部分——定义常量.变量.复杂数据类型.游标.用户自定义异常 */ ...