标签:public value ges 3.x span return warning 更改 --
刚之前没什么区别,多了一个applicationContext.xml
由于之前出了一点错误,有些包可能多加上了
User.java
package com.ij34.model; public class User { private int id; private String name; private int age; 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
UserMapper.java
package com.ij34.model; public interface UserMapper { public User selectUser(int id); public void insertUser(User user); public void updateUser(User user); public void deleteUser(String name); }
UserMapper.xml
<?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.ij34.model.UserMapper"> <select id="selectUser" parameterType="int" resultType="User"> select * from users where id=#{id}; </select> <update id="updateUser" keyProperty="id"> update users set name=#{name},age=#{age} where id=#{id} </update> <insert id="insertUser" > insert into users(name,age)values(#{name},#{age}) </insert> <delete id="deleteUser" > delete from users where name=#{name} </delete> </mapper>
mybatis-config.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> <typeAliases> <typeAlias type="com.ij34.model.User" alias="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://localhost:3306/mybatis?useSSL=true"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> --> <mappers> <mapper resource="com/ij34/mybatis/UserMapper.xml"/> <!-- <mapper class="com.ij34.model.UserMapper"/> --> </mappers> </configuration>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- Showcase‘s CustomFreemarkerManager example --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="com/ij34/mybatis/mybatis-config.xml"></property> </bean> <bean id="UserMapperFactory" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> <property name="mapperInterface" value="com.ij34.model.UserMapper"></property> </bean> </beans>
package com.ij34.bean; import java.io.IOException; //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 org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ij34.model.*; public class Test { public static void main(String[] args) throws IOException { @SuppressWarnings("resource") ApplicationContext ac=new ClassPathXmlApplicationContext("com/ij34/mybatis/applicationContext.xml"); UserMapper mapper=(UserMapper) ac.getBean("UserMapperFactory"); User user=mapper.selectUser(3); //查找 System.out.println(user); /* User user=new User(); user.setId(9); user.setName("小测01"); user.setAge(22); mapper.insertUser(user); //添加 System.out.println(user);*/ /* User user=mapper.selectUser(8); user.setName("大大A"); user.setAge(28); mapper.updateUser(user); //更改 System.out.println(user);*/ // mapper.deleteUser("小测01"); //删除 // System.out.println("已经删除:小测01"); } }
---------多动手,少打字
MyBatis笔记----Mybatis3.4.2与spring4整合:增删查改
标签:public value ges 3.x span return warning 更改 --
原文地址:http://www.cnblogs.com/tk55/p/6670155.html