MyBatis
编程技术  /  houtizong 发布于 3年前   133
<?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> <typeAliases> <typeAlias alias="User" type="com.jiang.mybatis.model.User"/> </typeAliases> <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://127.0.0.1:3306/mybatis" /> <property name="username" value="root"/> <property name="password" value="1234"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/jiang/mybatis/model/User.xml"/> </mappers></configuration>
package com.jiang.mybatis.model;public class User { private int id; private String userName; private String userAge; private String userAddress; 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 getUserAge() { return userAge; } public void setUserAge(String userAge) { this.userAge = userAge; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; }}
<?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.jiang.mybatis.models.UserMapper"> <!-- 注意:此处resultType的User为Configuration里的别名。 实体的属性和数据库中的字段名相同时会自动匹配,如果不同则需要配置一个自己的resultMap然后指定一个返回类型为resultMap的自定义Map --> <select id="selectUserByID" parameterType="int" resultType="User"> select * from user where id = #{id} </select></mapper>
package test;import java.io.Reader;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.jiang.mybatis.model.User;public class Test {private static SqlSessionFactory sqlSessionFactory;private static Reader reader;static {try {reader = Resources.getResourceAsReader("Configuration.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);} catch (Exception e) {e.printStackTrace();}}public static SqlSessionFactory getSession() {return sqlSessionFactory;}public static void main(String[] args) {SqlSession session = sqlSessionFactory.openSession();try {User user = (User) session.selectOne("com.jiang.mybatis.models.UserMapper.selectUserByID", 1);System.out.println(user.getUserAddress());System.out.println(user.getUserName());} finally {session.close();}}}
package com.jiang.mybatis.inter;import com.jiang.mybatis.model.User;public interface IUserOperation { //这里的方法名必须和映射文件里的select的id相同public User selectUserByID(int id);}
...public static void main(String[] args) {SqlSession session = sqlSessionFactory.openSession();try {IUserOperation userOperation = session.getMapper(IUserOperation.class);User user = userOperation.selectUserByID(1);System.out.println(user.getUserAddress());System.out.println(user.getUserName());} finally {session.close();}}...
package com.jiang.mybatis.inter;import java.util.List;import com.jiang.mybatis.model.User;public interface IUserOperation { //这里的方法名必须和映射文件里的select的id相同public User selectUserByID(int id);public List<User> selectUsers(String userName);public void addUser(User user);public void updateUser(User user); public void deleteUser(int id);}
<?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.jiang.mybatis.inter.IUserOperation"> <resultMap type="User" id="resultListUser"> <id column="id" property="id" /> <result column="user_name" property="userName" /> <result column="user_age" property="userAge" /> <result column="user_address" property="userAddress" /> </resultMap> <select id="selectUserByID" parameterType="int" resultMap="resultListUser"> select * from user where id = #{id} </select> <!-- 返回list 的select 语句,注意 resultMap 的值是指向前面定义好的 --> <select id="selectUsers" parameterType="string" resultMap="resultListUser"> select * from user where user_name like #{userName} </select> <!--id和parameterType分别与IUserOperation接口中的addUser方法的名字和参数类型一致; 以#{name}的形式引用User参数的属性,MyBatis将使用反射读取Student参数的此属性,#{name}中name大小写敏感; seGeneratedKeys设置为"true"表明要MyBatis获取由数据库自动生成的主键; keyProperty="id"指定把获取到的主键值注入到User的id属性--> <insert id="addUser" parameterType="User" useGeneratedKeys="true" keyProperty="id"> insert into user(user_name,user_age,user_address) values (#{userName},#{userAge},#{userAddress}) </insert> <update id="updateUser" parameterType="User" > update user set user_name=#{userName},user_age=#{userAge},user_address=#{userAddress} where id=#{id} </update> <delete id="deleteUser" parameterType="int"> delete from user where id=#{id} </delete></mapper>
package test;import java.io.Reader;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.jiang.mybatis.inter.IUserOperation;import com.jiang.mybatis.model.User;public class Test {private static SqlSessionFactory sqlSessionFactory;private static Reader reader;static {try {reader = Resources.getResourceAsReader("Configuration.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {Test test = new Test();test.selectUserByID();test.selectUsers();test.addUser();test.updateUser();test.deleteUser();}public void selectUserByID() {SqlSession session = sqlSessionFactory.openSession();try {IUserOperation userOperation = session.getMapper(IUserOperation.class);User user = userOperation.selectUserByID(1);System.out.println("selectUserByID: "+user.getUserAddress()+":"+user.getUserName());} finally {session.close();}}public void selectUsers() {SqlSession session = sqlSessionFactory.openSession();try {IUserOperation userOperation = session.getMapper(IUserOperation.class);List<User> users = userOperation.selectUsers("AAA");for (User user : users) {System.out.println("selectUsers: "+user.getId() + ":" + user.getUserName()+ ":" + user.getUserAddress());}} finally {session.close();}}public void addUser(){ User user=new User(); user.setUserAddress("人民广场"); user.setUserName("飞鸟"); user.setUserAge("33"); SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation=session.getMapper(IUserOperation.class); userOperation.addUser(user); session.commit(); //增加必须提交事务,否则不会写入库中。 System.out.println("addUser: id="+user.getId()); } finally { session.close(); } }public void updateUser(){ //先得到用户,然后修改,提交。 SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation=session.getMapper(IUserOperation.class); User user = userOperation.selectUserByID(1); user.setUserAddress("MMMMMMMM"); userOperation.updateUser(user); session.commit(); } finally { session.close(); } }public void deleteUser(){ SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation=session.getMapper(IUserOperation.class); userOperation.deleteUser(1); session.commit(); } finally { session.close(); } }}
package com.jiang.mybatis.model;public class Article { private int id; private User user; private String title; private String content; ....}
<?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.jiang.mybatis.inter.IUserOperation"> <resultMap type="User" id="resultListUser"> <id column="id" property="id" /> <result column="user_name" property="userName" /> <result column="user_age" property="userAge" /> <result column="user_address" property="userAddress" /> </resultMap> <resultMap id="resultUserArticleList" type="Article"> <id property="id" column="aid" /> <result property="title" column="title" /> <result property="content" column="content" /> <association property="user" javaType="User" resultMap="resultListUser" /> </resultMap> <sql id="Base_Column_List"> user.id,user.user_name,user.user_address,article.id aid,article.title,article.content</sql> <select id="getUserArticles" parameterType="int" resultMap="resultUserArticleList"> select <include refid="Base_Column_List"/> from user,article where user.id=article.userid and user.id=#{id} </select></mapper>
public void getUserArticles(int userid){ SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation=session.getMapper(IUserOperation.class); List<Article> articles = userOperation.getUserArticles(userid); for(Article article:articles){ System.out.println(article.getTitle()+" : "+article.getUser().getUserName()); } } finally { session.close(); } }
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接