mybatis的简单使用
需要用到的包:(这里只是其中一个版本,其他的百度) mysql-connector-java-5.1.6-bin mybatis-3.2.2 先看项目目录: 配置文件mybatisconfig.xml: ?xml version=1.0 encoding=UTF-8?!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp:/
需要用到的包:(这里只是其中一个版本,其他的百度)
mysql-connector-java-5.1.6-bin
mybatis-3.2.2
先看项目目录:
配置文件mybatisconfig.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 用XML映射的时候必须配此项!指定mappers里面的返回值对应的实体类 --> <!-- <typeAliases> <typeAlias alias="User" type="com.miquan.mybatis.bean.User"/> </typeAliases> --> <!-- JDBC配置 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/weixinface"/> <property name="username" value="root"/> <property name="password" value=""/> </dataSource> </environment> </environments> <!-- mappers,每个实体类对应着一个xml --> <mappers> <mapper resource="com/miquan/mybatis/bean/User.xml"/> </mappers> </configuration>
package com.miquan.mybatis.bean; public class User { private int id; private String userName; private String password; public User(int id, String userName, String password) { super(); this.id = id; this.userName = userName; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
<?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"> <!-- namespace对应着接口类 --> <mapper namespace="com.miquan.mybatis.inter.IUserOperation"> <!-- <select id="selectById" parameterType="int" resultType="User"> select * from User where id = #{id} </select> --> </mapper>
package com.miquan.mybatis.inter; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.miquan.mybatis.bean.User; public interface IUserOperation { @Select("select * from User where id = #{id}") public User selectById(int id); @Select("select * from User where userName = #{userName}") public List<User> selectUsers(String userName); @Insert("insert into User values(null, #{userName}, #{password})") public boolean addUser(User user); @Delete("delete from User where id = #{id}") public boolean delUser(int id); @Update("update User " + "set userName = #{userName}, " + "password = #{password} " + "where id = #{id}") public boolean updateUser(User user); } 最后进行测试:Test.java:
package com.miquan.mybatis.test; import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.miquan.mybatis.bean.User; import com.miquan.mybatis.inter.IUserOperation; public class Test { static SqlSession session; public static void main(String[] args) { try { //获取session InputStream is = Resources.getResourceAsStream("mybatisconfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); session = sqlSessionFactory.openSession(); //XML配置对应用法 // User user = session.selectOne("com.miquan.mybatis.bean.UserDB.selectById", 1); // System.out.println(user.toString()); //接口调用法 //////注意:insert、delete和update方法调用之后必须commit才能真正的改变数据 IUserOperation userOperation = session.getMapper(IUserOperation.class); //查 // User user = userOperation.selectById(1); // System.out.println(user.toString()); //查找List // List<User> users = userOperation.selectUsers("miquan"); // System.out.println(users.size()); //增 // User user = new User(); // user.setUserName("zhiquan"); // user.setPassword("999"); // userOperation.addUser(user); // session.commit(); //删 // userOperation.delUser(7); // session.commit(); //改 User user = new User(8, "qiantu", "shaxppp"); userOperation.updateUser(user); session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { try { session.close(); } catch (Exception e) { e.printStackTrace(); } } } } 简单的增删查改可以实现了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

With the rapid development of network technology, our lives have also been greatly facilitated, one of which is the ability to download and share various resources through the network. In the process of downloading resources, magnet links have become a very common and convenient download method. So, how to use Thunder magnet links? Below, I will give you a detailed introduction. Xunlei is a very popular download tool that supports a variety of download methods, including magnet links. A magnet link can be understood as a download address through which we can obtain relevant information about resources.

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension
