电脑中的中文输入花的双引号怎样输入豆号,句

mybatis(2)
这种情况属于增删改查条件为集合时遇到。例如想要删除10个用户,根据id删除,如果每次只传入一个id,那么需要执行10条delete语句,如果利用foreach后一条语句就搞定,下面列举出一个简单的方法,
mapper里面的类方法:
(1)public void delRelative(PatientRelativeDto ratientRelativeDto) throws E
与其对应的xml语句:
&delete id=&delRelative& parameterType=&PatientRelativeDto&&
& & & & delete from &t_patient_relative
& & & & where patientId = #{patientId} and id in
&foreach collection=&idList& item=&id& index=&index& open=&(& close=&)& separator=&,&&
& & & & & & &#{id}
& & & & &&/foreach&
& & &/delete&
解释:(1)中的参数ratientRelativeDto是一个实体类,里面有属性patientId 和idList集合,这两个参数在(2)中作为参数;注意:collection等于的值必须是实体类中含有的属性,这里是集合idList,否则sql会报错,
foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名.
index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置.
open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符.
close表示以什么结束.
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:7592次
排名:千里之外
原创:21篇
(4)(1)(5)(2)(1)(2)(2)(1)(2)(1)(1)(1)generator自动生成mybatis的xml配置 - 博客频道 - CSDN.NET
一万年太久 -
孤独的追梦人
分类:mybatis -学习
generator自动生成mybatis的xml配置、model、map等信息:
1、下载mybatis-generator-core-1.3.2.jar包。
&&&&&& 网址:,下载mybatis-generator-core-1.3.2-bundle.zip,解压
&&&&&& 找到lib下的需要jar包。
2、编写genertor的xml文件,名下:generator.xml
&?xml version=&1.0& encoding=&UTF-8&?&
&!DOCTYPE generatorConfiguration
PUBLIC &-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN&
&http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd&&
&generatorConfiguration&
&!-- classPathEntry:数据库的JDBC驱动的jar包地址--&
&classPathEntry location=&E:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar& /&
&context id=&DB2Tables& targetRuntime=&MyBatis3&&
&commentGenerator&
&!-- 是否去除自动生成的注释 true:是 : false:否 --&
&property name=&suppressAllComments& value=&true& /&
&!--数据库连接的信息:驱动类、连接地址、用户名、密码 --&
&/commentGenerator&
&jdbcConnection driverClass=&oracle.jdbc.driver.OracleDriver&
connectionURL=&jdbc:oracle:thin:@198.17.1.1:1521:ORCL&
userId=&unuser&
password=&password&&
&/jdbcConnection&
默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
&javaTypeResolver &
&property name=&forceBigDecimals& value=&false& /&
&/javaTypeResolver&
&!-- targetProject:自动生成代码的位置 --&
&javaModelGenerator targetPackage=&com.soft.model& targetProject=&E:\WebWorkSpace\workspace_js\downAttachdemo\src&&
&!-- enableSubPackages:是否让schema作为包的后缀 --&
&property name=&enableSubPackages& value=&true& /&
&!-- 从数据库返回的值被清理前后的空格
&property name=&trimStrings& value=&true& /&
&/javaModelGenerator&
&sqlMapGenerator targetPackage=&sqlmap&
targetProject=&E:\WebWorkSpace\workspace_js\downAttachdemo\conf&&
&property name=&enableSubPackages& value=&false& /&
&/sqlMapGenerator&
&javaClientGenerator type=&XMLMAPPER& targetPackage=&com.soft.mapping&
targetProject=&E:\WebWorkSpace\workspace_js\downAttachdemo\src&&
&property name=&enableSubPackages& value=&true& /&
&/javaClientGenerator&
&!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 --&
&table schema=&untodo& tableName=&mocha_t_app& domainObjectName=&MochaTodoApp& &
&/context&
&/generatorConfiguration&
able其他属性:
enableCountByExample=&false&
enableUpdateByExample=&false&
enableDeleteByExample=&false&
enableSelectByExample=&false&
selectByExampleQueryId=&false&
schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类,
如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true,
这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时,
就不会生成对应的Example类了.
如果table里边不配置property,默认字段都生成为类属性。
&ignoreColumn column=&FRED& /&//忽略字段
&columnOverride column=&LONG_VARCHAR_FIELD& jdbcType=&VARCHAR& /&//无论字段是什么类型,生成的类属性都是varchar。
3、运行有四种:命令生成(最简单)、Java生成、ant生成、maven生成。这里说两种,有兴趣其余的可以在mybatis官网去学习。
1)、运行-》cmd-&java - jar jar包的文件路径& -configfile& generator.xml的文件路径& -overwrite 命令。
java -jar E:\Websoft\mybaits\mybatis-generator-core-1.3.2\lib\mybatis-generator-core-1.3.2.jar -configfile E:\WebWorkSpace\workspace_js\downAttachdemo\src\com\mochasoft\down\generator.xml -overwrite
成功时输出:MyBatis Generator finished successfully.
2)、java运行关键代码:
List&String& warnings = new ArrayList&String&();
boolean overwrite =
File configFile = new File(&generatorConfig.xml&);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
其实Java运行,细分可以分两种,还有一种可以去官网学习。
4、生成代码之后,根据自己的实际项目架构,可以对生成的代码进行适当的修改,如把数据库管理交有spring等等。
superdog007
排名:第3490名
(29)(0)(5)(1)(57)(4)(1)(3)(1)(7)(24)(2)(5)(1)(2)(1)(2)(9)(1)(0)(1)(1)(1)(3)(2)(2)(1)(1)(2)(1)(1)(1)(1)(0)(1)(1)(2)(1)(2)(2)(2)(0)(1)(1)(2)(4)(1)(0)(0)(1)(1)(2)(1)MyBatis学习系列二——增删改查 - daixinet - 博客园
数据库的经典操作:增删改查。
在这一章我们主要说明一下简单的查询和增删改,并且对程序接口做了一些调整,以及对一些问题进行了解答。
1、调整后的结构图:
2、连接数据库文件配置分离:
&  一般的程序都会把连接数据库的配置单独放在.properties 文件中,然后在XML文件中引用,示例如下:
  config.properties:
driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=phonesurvey
password=world
  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&
&properties resource="config.properties" /&
&environments default="development"&
&environment id="development"&
&transactionManager type="JDBC" /&
&dataSource type="POOLED"&
&property name="driver" value="${driver}"/&
&property name="url" value="${url}"/&
&property name="username" value="${username}"/&
&property name="password" value="${password}"/&
&/dataSource&
&/environment&
&/environments&
&mapper resource="nankang/dao/agentDao.xml" /&
&/mappers&
&/configuration&
3、SqlSession分离:
&  SqlSeesion单独做成工具类,以便调用,示例如下:
SqlSessionHelper:
package nankang.
import java.io.InputS
import org.apache.ibatis.io.R
import org.apache.ibatis.session.SqlSessionF
import org.apache.ibatis.session.SqlSessionFactoryB
public class SqlSessionHelper {
public static SqlSessionFactory getSessionFactory(){
SqlSessionFactory sessionFactory = null;
String resource= "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//Reader reader = Resources.getResourceAsReader(resource);
sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}catch(Exception ex){
ex.printStackTrace();
return sessionF
  SqlSessionFactory创建时,根据Reader和InputStream都可以。
4、XML文件添加内容:
  &agentDao.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="nankang.dao.AgentDao"&
&!-- 根据Id查询 --&
&select id="selectAgentById" parameterType="string" resultType="nankang.po.Agent"&
select * from Agent where AgentId=#{id}
&!-- 添加 --&
&insert id="insertAgent" parameterType="nankang.po.Agent"&
insert into Agent(agentId, companyCode, LoginName, AgentPwd, AgentCode, Name, status,sysFlag)
values(#{agentId},'SHNK',#{loginName},'D41D8CD98F00B204E9800998ECF8427E',#{agentCode},#{name},1,1)
&!-- 删除 --&
&delete id="deleteAgent" parameterType="string"&
delete from Agent where agentid=#{id}
&!-- 修改 --&
&update id="updateAgent" parameterType="nankang.po.Agent"&
update agent set name=#{name} where agentid=#{agentId}
&!-- 查询所有 --&
&select id="selectAllAgent" resultType="nankang.po.Agent"&
select * from Agent
&!-- 查询所有无返回对象 --&
&select id="selectAllAgent2" resultType="hashmap"&
select * from Agent
AgentDao.java:
package nankang.
import java.util.L
import java.util.M
import nankang.po.A
import org.apache.ibatis.annotations.S
public interface AgentDao {
//根据Id查询
public Agent selectAgentById(String Id);
//根据名称查询
@Select("select * from Agent where name=#{name}")
public Agent selectAgentByName(String name);
public int insertAgent(Agent agent);
public int deleteAgent(String id);
public int updateAgent(Agent agent);
//查询所有的
public List&Agent& selectAllAgent();
public List&Map&String, Object&& selectAllAgent2();
  1、XML文件中的语句,可以直接写在接口文件中,如:根据名称查询;
  2、其他参考示例。
  几个问题说明:
  1)如何查询数据集合?
    使用ResultType设置,返回用List&T&即可
  2)查询一条数据,如果为空,怎么判断?
    如果没有查询到数据,返回为NULL,进行空对象判断即可
  3)查询所有的集合,不放在构建对象的List中:
    resultType=map,返回类型 List&Map&String,Object&&,字段为空则不展示在Map中
  4)如何实现事务:
    SqlSession:commit,rollback,close
package nankang.
import java.util.L
import java.util.M
import nankang.dao.AgentD
import nankang.util.SqlSessionH
import org.apache.ibatis.session.SqlS
public class test {
* @param args
public static void main(String[] args) {
SqlSession sqlSession = SqlSessionHelper.getSessionFactory().openSession();
AgentDao agentMapper = sqlSession.getMapper(AgentDao.class);
//根据Id查询
Agent agent = agentMapper.selectAgentById("SHNKAG");
if(null != agent){
System.out.println(agent.getName());
System.out.println("不存在该用户");
agent = agentMapper.selectAgentByName("1001");
System.out.println(agent.getAgentId());
//查询所有
List&Map&String, Object&& agentList = agentMapper.selectAllAgent2();
System.out.println(agentList.size());
Format format = new SimpleDateFormat("00yyyyMMddhhmmss");
Calendar calendar = Calendar.getInstance();
String dateStr = format.format(calendar.getTime());
Agent agent = new Agent();
agent.setAgentId(dateStr);
agent.setLoginName("1111");
agent.setAgentCode("aaaa");
agent.setName("aaaa");
int num = agentMapper.insertAgent(agent);
System.out.println(num);
int num = agentMapper.deleteAgent("3127");
System.out.println(num);
Agent agent = new Agent();
agent.setAgentId("0005");
agent.setName("Test");
int num = agentMapper.updateAgent(agent);
System.out.println(num);
//增删改,提交
System.out.println("完成");
}catch(Exception ex){
sqlSession.rollback();
System.out.println(ex.getMessage());
sqlSession.close();
  这边需要注意的是:SqlSession一定要close
6、源码下载:&(Fish的分享&MyBatis&myBatis2.rar)

我要回帖

更多关于 中文输入花的双引号 的文章

 

随机推荐