Spring+springmvc+mybatis Integration Framework additions and Deletions check demo (including resource download link)

Source: Internet
Author: User

Source code download link (test available)

First, the operating environment

Myeclipse+tomcat+mysql

Second, the project package structure


Three, Entity entity package

<span style= "FONT-SIZE:14PX;" >package com.tccp.entity;

/** *
 User
 * @author TCCP project Group
 * * *
/public class User {

	private int user_id;
	Private String username;
	Private String age;
	
	Public User () {
		super ();
	}
	
	Public User (int user_id, string username, String age) {
		super ();
		this.user_id = user_id;
		This.username = Username;
		This.age = age;
	}

	public int getuser_id () {return
		user_id;
	}

	public void setuser_id (int user_id) {
		this.user_id = user_id;
	}

	Public String GetUserName () {return
		username;
	}

	public void Setusername (String username) {
		this.username = username;
	}

	Public String Getage () {return age
		;
	}

	public void Setage (String age) {
		this.age = age;
	}

	@Override public
	String toString () {return
		"User [user_id=" + user_id + ", username=" + username + 
				", age= "+ Age +"] ";
	}
		
} </span><span style= "FONT-SIZE:18PX;" >
</span>
Third, persistence persistence layer

Interface class:

<span style= "FONT-SIZE:14PX;" >package com.tccp.persistence;

Import java.util.List;
Import Com.tccp.entity.User;

Public interface Usermapper {
	//
	add-N to check void adding (user user);
	Boolean update (user user);
	Boolean delete (int user_id);
	User FindByID (int user_id);
	List<user> findall ();
	
}
</span>
XML configuration file:

<span style= "FONT-SIZE:14PX;" ><?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" > <!--N Amespace: Must be consistent with the corresponding interface full class name ID: must correspond to the corresponding interface of a corresponding method name--> <mapper namespace= "Com.tccp.persistence.UserMapper" > < !--the alias class name configured in Mybsits_config, you can also configure Resulttype as the classpath--> <insert id= "Add" parametertype= "User" > INSERT INTO US ER (username, age) VALUES (#{username},#{age}) </insert> <update id= "Update" parametertype= "User" > Update User set Username=#{username},age=#{age} where user_id=#{user_id} </update> <delete id= "Delete" ParameterType = "int" > delete from user where user_id=#{user_id} </delete> <select id= "FindByID" parametertype= "int" Resulttype= "User" > select user_id user_id,username username,age age from User where user_id=#{user_id} &LT;/SELECT&G
	
	T <select id= "FindAll" resulttype= "User" > select USer_id user_id,username username,age age from user </select> </mapper></span> 
Iv. Database Operation Testing Test

Spring test:

<span style= "FONT-SIZE:14PX;" >package com.tccp.test;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

public class Springtest {public

	 static void Main (string[] args) {
		 ApplicationContext ctx = new Classpathxmlapplic Ationcontext ("Config/spring-common.xml");
		 Object usermapper = Ctx.getbean ("Usermapper");
		 System.out.println (Usermapper);
	 }
</span><span style= "FONT-SIZE:18PX;" >
</span>
Database Operation Test:

<span style= "FONT-SIZE:14PX;"


>package com.tccp.test;

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.tccp.persistence.UserMapper;

Import Com.tccp.entity.User; @RunWith (Springjunit4classrunner.class) @ContextConfiguration ("/config/spring-common.xml") public class Usertest {@
	
	autowired private Usermapper Usermapper;
		@Test public void Testadd () {User user = new User (4, "Harry", "33");
		
	Usermapper.add (user);
		@Test public void Testfindall () {list<user> findalllist = Usermapper.findall ();
	System.out.println (Findalllist.size ());
		@Test public void Testfindbyid () {User user = Usermapper.findbyid (2);
		System.out.println (user.getuser_id ());
	System.out.println (User.getusername ()); } @Test public void TESTUPDATe () {User user = new User (5, "Money 6", "24");
	Usermapper.update (user);
	@Test public void Testdelete () {usermapper.delete (4); }} </span>
(configuration file and business Logic Layer code slightly)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.