标签:html use str stack 同方 rtu java 忘记 update
注意,这篇文章只介绍mybatis单独使用时如何操作,是没有用到spring的,如果需要了解mybatis和spring如何搭建,请移步这里Mybatis(六):spring与mybatis三种整合方法。
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency>
<?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> <settings> <!-- 打印查询语句 --> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> <!-- 和Spring整合后environment配置都会被干掉 --> <environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理,目前由mybatis来管理 --> <transactionManager type="JDBC" /> <!-- 数据库连接池,目前由mybatis来管理 --> <dataSource type="POOLED"><!--有关于mysql数据库的各种信息--> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <!--将操作配置文件User.xml系添加进mapper--> <mapper resource="mybatis/user.xml" /> </mappers> </configuration>
更多的参数配置,参阅:Mybatis(一):MyBatis配置文件config.xml详解
public class User { private String id; private String password; private String username; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPassword(){ return password; } public void setPassword(String password){ this.password=password; } public String getUsername(){ return username; } public void setUsername(String username){ this.username=username; } }
<?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="user"> <!-- 注意,因为这边没有用到mapper接口,所以这里的namespace不需要是完全的类名 --> <!-- 通过id查询用户 --> <select id="findUserById" parameterType="int" resultType="com.mvc.User"> <include refid="selectStr"/> id = #{id} </select> <!--通过name查找一个list的用户,模糊匹配--> <select id="findUserByName" parameterType="java.lang.String" resultType="com.mvc.User"> select * from user where name like ‘%${value}%‘ </select> <!--插入用户信息--> <insert id="insertUser" parameterType="com.mvc.User"> <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> select uuid() <!-- 这里是对于主键属性的id进行赋值 --> </selectKey> insert into user(id,username,password) values(#{id},#{username},#{password}) </insert> <!--删除用户信息--> <delete id="deleteUser" parameterType="java.lang.Integer"> delete from user where id=#{id} </delete> <!--更新用户信息--> <update id="updateUser" parameterType="com.mvc.User"> <!-- update user set name=#{name},password=#{password} where id=#{id} --> update user <set> <if test="username != null && username != ‘‘">username=#{username},</if> <if test="password != null && password != ‘‘">password=#{password},</if> </set> where id= #{id} </update> </mapper>
import java.io.IOException; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybatisSessionUtil { static SqlSessionFactory sqlSessionFactory = null; static{ SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); try { sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml")); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSession(){ return sqlSessionFactory.openSession(); } }
import org.apache.ibatis.session.SqlSession; import com.mvc.User; import com.mybatis.mapper.User2Mapper; public class UserDao { SqlSession session = MybatisSessionUtil.getSession(); public int insert(User user){ int rint = session.insert("user.insertUser",user); // 第一个参数是mapper xml里的namespace+MappedStatement对应的id session.commit();// 不要忘记提交 return rint; } }
import com.mvc.User; import com.mybatis.util.UserDao; public class MybatisNoSpringTest { public static void main(String[] args) { UserDao userDao = new UserDao(); User user = new User(); user.setUsername("wwwttt"); user.setPassword("33333"); if (userDao.insert(user) == 1) { System.out.println("insert success..."); }else{ System.out.println("insert fail..."); } } }
同方式一
同方式一,就是换成新的user mapper xml
<mappers> <!--将操作配置文件系添加进mapper--> <mapper resource="mybatis/User2Mapper.xml" /> </mappers>
同方式一
import com.mvc.User; // 这边接口名要和xml映射的文件名一致 public interface User2Mapper { int insertUser(User user); // 其它方法略。。。 }
同方式一,只是namespace需要注意
<?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.mybatis.mapper.User2Mapper"> <!-- 通过id查询用户 --> <select id="findUserById" parameterType="int" resultType="com.mvc.User"> select * from user where id = #{id} </select> <!--通过name查找一个list的用户,模糊匹配--> <select id="findUserByName" parameterType="java.lang.String" resultType="com.mvc.User"> select * from user where name like ‘%${value}%‘ </select> <!--插入用户信息--> <insert id="insertUser" parameterType="com.mvc.User"> <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> select uuid() <!-- 这里是对于主键属性的id进行赋值 --> </selectKey> insert into user(id,username,password) values(#{id},#{username},#{password}) </insert> <!--删除用户信息--> <delete id="deleteUser" parameterType="java.lang.Integer"> delete from user where id=#{id} </delete> <!--更新用户信息--> <update id="updateUser" parameterType="com.mvc.User"> update user set name=#{name},password=#{password} where id=#{id} </update> </mapper>
同方式一
import org.apache.ibatis.session.SqlSession; import com.mvc.User; import com.mybatis.mapper.User2Mapper; public class UserDao { SqlSession session = MybatisSessionUtil.getSession(); // 使用接口方式 public int insert2(User user){ User2Mapper user2Mapper = session.getMapper(User2Mapper.class); int rint = user2Mapper.insertUser(user); session.commit(); return rint; } }
import com.mvc.User; import com.mybatis.util.UserDao; public class MybatisNoSpringTest { public static void main(String[] args) { UserDao userDao = new UserDao(); User user = new User(); user.setUsername("wwwttt"); user.setPassword("33333"); // 使用mapper接口方式 if (userDao.insert2(user) == 1) { System.out.println("insert success..."); }else{ System.out.println("insert fail..."); } } }
这种方式,我不推荐使用,本来我们使用mybatis就是为了sql编写和java代码能分开解耦,有更大的灵活性,使用这种方式反而不好。
另外,myatis官方也不推荐这种方式,官方说了,要想使用mybatis强大的sql编写功能,写在注解里是受到限制的,只有写在mapper xml里才能发挥的玲离尽致。
所以,这种方法我就不记录了,有需要的同学自行百度。
标签:html use str stack 同方 rtu java 忘记 update
原文地址:https://www.cnblogs.com/shamo89/p/9960370.html